Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 dynamic increment of columns

Author  Topic 

skillile
Posting Yak Master

208 Posts

Posted - 2001-11-27 : 09:31:03
I have a table with the columns:
level1lab
level2lab
lev...to lev10lab

I want to execute a select like this

declare @offstep int
declare @x nvarchar(15)
declare @str varchar(500)

SET @offstep=6
SET @x=1

while @x <= @offstep
begin
set @str= ' level'+ @x+'lab' + ' from tblcorpprof'
select @str

SET @x= @x+1
end

I can't get it to execute the values just the text.

Any help. Thnanks



slow down to move faster...

btrimpop
Posting Yak Master

214 Posts

Posted - 2001-11-27 : 09:32:50
Do it dynamically, something like this:

set @str= 'Select level'+ @x+'lab' + ' from tblcorpprof'
exec(@str)



"In theory there is no difference between theory and practice. But in practice there is!"



Go to Top of Page

skillile
Posting Yak Master

208 Posts

Posted - 2001-11-27 : 09:35:29
worked great.
Thanks ;)

slow down to move faster...
Go to Top of Page

shankarc
Starting Member

37 Posts

Posted - 2001-11-27 : 09:35:32
Try executing the SQL statement using sp_executeSQL or Exec. Please refer to dynamic SQL in the SQLteam.com Articles.

Go to Top of Page
   

- Advertisement -