adding new columns in run time

  • Hello, i wonder if can i add new columns in run time usign T-SQL, the name of the column is a variable, i've tried this:

    declare @v-2 char(1)

    set @v-2='s'

    alter table table1

    add @v-2 int

    but it's not work, is this no possible in sqlserver? or how i do it? thanks

  • I think you need to use dynamic SQL to get this done at runtime...

    something like this :

    Declare @sql NVarchar(500)

    Declare @v-2 Char(1)

    SET @v-2 = 's'

    SET @sql = 'Alter table table1 add '+@V+' int'

    Print @sql -- for testing/debugging

    Exec sp_executesql @sql

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply