Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2003
    Location
    Montevideo, Uruguay, South America
    Posts
    13

    Wink Unanswered: Add column with variable name

    HI!

    Im trying to use an stored procedure to add a new column.
    The problem is that the column name is stored in a variable, and I get syntax error.

    Here is my code:

    CREATE PROCEDURE test AS
    declare @colname as varchar(10)

    set @colname = 'new_col'

    alter table prueba add @colname int

    And here is the error I get:
    Error 170: Line 6 Incorrect syntax near '@colname'

    Any idea???
    Bye
    Lorena

  2. #2
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621

    Re: Add column with variable name

    Originally posted by soy_lore_lore
    HI!

    Im trying to use an stored procedure to add a new column.
    The problem is that the column name is stored in a variable, and I get syntax error.

    Here is my code:

    CREATE PROCEDURE test AS
    declare @colname as varchar(10)

    set @colname = 'new_col'

    alter table prueba add @colname int

    And here is the error I get:
    Error 170: Line 6 Incorrect syntax near '@colname'

    Any idea???
    Try this:


    exec('alter table prueba add '+@colname+' int')

  3. #3
    Join Date
    Oct 2003
    Location
    Montevideo, Uruguay, South America
    Posts
    13

    Talking Re: Add column with variable name

    Originally posted by snail
    Try this:


    exec('alter table prueba add '+@colname+' int')

    THANKS!
    Bye
    Lorena

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •