Groups | Blog | Home
all groups > sql server programming > june 2004 >

sql server programming : How to create varialbles dynamically?



Soon
6/8/2004 11:41:01 PM
declare @cntColumn in
...
...
while @@fetch_status =
begi
set @cntColumn = @cntColumn +
set @strParam = @strParam + N'declare @column' + Cast(@cntColumn as varchar(10)) + ' varchar(50)
set @strParam = @strParam + N'Set @column' + Cast(@cntColumn as varchar(10)) + '=''' + @paraName + '''
en
exec sp_executesql @strPara

declare @i in
set @i =
set @strColumn = N'
while @i <= @cntColum
begi
set @strColumn = @strColumn + N'Select @column' + Cast(@i as varchar(10)) + char(10
set @i = @i +
en
Print @strColum
exec sp_executesql @strColum
...
...
I wish to create variables dynamically,set the variables data and retrieve the data..

but error shown
Must declare the variable '@column1'
Must declare the variable '@column2'
Must declare the variable '@column3'

could anybody senior here help me..
any method to do this

thanks.

regards

Soon, Junior Programme

SriSamp
6/9/2004 12:25:05 PM
You cannot create variables dynamically in SQL Server. If you need to create
variables dynamically and refer to them, you will need to form the whole
block (variable creation and access semanticsc) as a dynamic SQL script and
then execute it.
--
HTH,
SriSamp
Please reply to the whole group only!
http://www32.brinkster.com/srisamp

[quoted text, click to view]

AddThis Social Bookmark Button