Saturday, February 25, 2012

Alter a table by using a variable as column name

Hi All,

i would like to alter a table by using a @.Variable as Column name like this:

Code Snippet

DECLARE @.VARIABLE AS nvarchar(50)

SET @.VARIABLE = 'Column1'

ALTER TABLE #TempTable ADD


@.VARIABLE AS nvarchar(50)

But it looks like, that i cant alter a table by using a variable as column name?

Can someone help me out?

Greetings,

Andreas

hi,
you can use dynamic query e.g.

Code Snippet


DECLARE @.VARIABLE AS nvarchar(50)

SET @.VARIABLE = 'Column1'

declare @.str varchar(4000)
set @.str = 'ALTER TABLE #TempTable ADD ' + @.variable + ' nvarchar(50)'
exec(@.str)


- clintz|||

Well ... that was a fast help!

Big thanks to you, clintz.

I think, i should put some time into dynamic query Smile

No comments:

Post a Comment