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 char(1)

    set @v='s'

    alter table table1

    add @v 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 Char(1)

    SET @v = '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