Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. adding column names using the stored procedure.error in the @ character

adding column names using the stored procedure.error in the @ character

Scheduled Pinned Locked Moved Database
databasecsssql-serversysadminhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pankaj Garg
    wrote on last edited by
    #1

    i was trying to add the column names in the existing table using the stored procedure in sql server 2000. My stored procedure is CREATE PROCEDURE [dbo].[spInsert_Master] AS declare @les varchar(1000) delete tblmaster create table tblMaster(categoryname varchar(100) , casestudy varchar(100) , UnitInformation varchar(100) , locationname varchar(100)) declare les cursor for select lessonname from tblLessonMaster open les fetch next from les into @les while(@@fetch_status =0) begin fetch next from les into @les alter table tblMaster add @less varchar(500); select @les as lessonname end close les deallocate les I got the error in the following line alter table tblMaster add @less varchar(500); says error near @less i think i can't add the @ expression in the column name. any idea for the alternative ? I want to add the column names in the existing table using stored procedure.

    If you have an apple & I have an apple and we exchange our apples, then each of us will still have only one apple but if you have an idea & I have an idea and we exchange our ideas, then each of us will have two ideas!

    K 1 Reply Last reply
    0
    • P Pankaj Garg

      i was trying to add the column names in the existing table using the stored procedure in sql server 2000. My stored procedure is CREATE PROCEDURE [dbo].[spInsert_Master] AS declare @les varchar(1000) delete tblmaster create table tblMaster(categoryname varchar(100) , casestudy varchar(100) , UnitInformation varchar(100) , locationname varchar(100)) declare les cursor for select lessonname from tblLessonMaster open les fetch next from les into @les while(@@fetch_status =0) begin fetch next from les into @les alter table tblMaster add @less varchar(500); select @les as lessonname end close les deallocate les I got the error in the following line alter table tblMaster add @less varchar(500); says error near @less i think i can't add the @ expression in the column name. any idea for the alternative ? I want to add the column names in the existing table using stored procedure.

      If you have an apple & I have an apple and we exchange our apples, then each of us will still have only one apple but if you have an idea & I have an idea and we exchange our ideas, then each of us will have two ideas!

      K Offline
      K Offline
      Krish KP
      wrote on last edited by
      #2

      try replacing this line alter table tblMaster add @less varchar(500); with SET @Str = 'alter table tblMaster add ' + @less ' varchar(500);' Exec (@Str) also need to declare @Str variable at the begining

      Regards KP

      P 1 Reply Last reply
      0
      • K Krish KP

        try replacing this line alter table tblMaster add @less varchar(500); with SET @Str = 'alter table tblMaster add ' + @less ' varchar(500);' Exec (@Str) also need to declare @Str variable at the begining

        Regards KP

        P Offline
        P Offline
        Pankaj Garg
        wrote on last edited by
        #3

        how can i check duplicate column names exist or not in the table?

        If you have an apple & I have an apple and we exchange our apples, then each of us will still have only one apple but if you have an idea & I have an idea and we exchange our ideas, then each of us will have two ideas!

        K 1 Reply Last reply
        0
        • P Pankaj Garg

          how can i check duplicate column names exist or not in the table?

          If you have an apple & I have an apple and we exchange our apples, then each of us will still have only one apple but if you have an idea & I have an idea and we exchange our ideas, then each of us will have two ideas!

          K Offline
          K Offline
          Krish KP
          wrote on last edited by
          #4

          in SQL 2000 syscolumns contains all column names of each table and sysobjects contains all table names Value 'U' in the column sysobjects.xtype indicates the objects is user table id column in both tables are common which to be used for joining the two tables.

          Regards KP

          P 1 Reply Last reply
          0
          • K Krish KP

            in SQL 2000 syscolumns contains all column names of each table and sysobjects contains all table names Value 'U' in the column sysobjects.xtype indicates the objects is user table id column in both tables are common which to be used for joining the two tables.

            Regards KP

            P Offline
            P Offline
            Pankaj Garg
            wrote on last edited by
            #5

            can u tell me a query which gives me the result regarding ,whether the particular column name already exist or not in the table?

            If you have an apple & I have an apple and we exchange our apples, then each of us will still have only one apple but if you have an idea & I have an idea and we exchange our ideas, then each of us will have two ideas!

            A 1 Reply Last reply
            0
            • P Pankaj Garg

              can u tell me a query which gives me the result regarding ,whether the particular column name already exist or not in the table?

              If you have an apple & I have an apple and we exchange our apples, then each of us will still have only one apple but if you have an idea & I have an idea and we exchange our ideas, then each of us will have two ideas!

              A Offline
              A Offline
              Ashfield
              wrote on last edited by
              #6

              Assuming SQL Server 2000 if exists (select 1 from syscolumns where name = 'yourcolumnname' and id = object_id('yourtablename') begin select 'It exists' end else begin select 'its new' end this will also work with SQL Server 2005 but you should be using the sys.xxx views

              Bob Ashfield Consultants Ltd

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups