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)
 declaring a variable column name in cursor

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-27 : 11:08:49
ozlem writes "I am trying to use variable column name inside of cursor statement but it doesn't return column value.
Any help is appreciated.
Thanks.

Declare @UserId as integer
Declare @bSpotFx as varchar(35)
declare @sProductName as varchar(35)
declare @ssql as varchar(255)
declare @i as integer
select @i=1
declare cursorproduct Cursor

For
select productname from product_tbl
open cursorproduct
Fetch next from cursorproduct
into @sproductname
while (@@fetch_status <> -1)
begin
Declare cursorprofile Cursor For
select userid,('+ @sproductName +') from userprofile_tbl where
USERID>0
OPEN cursorProfile
FETCH NEXT from cursorprofile
Into @userid,@bSpotFx
WHILE (@@fetch_status <> -1)
BEGIN
print @i
print @userid
print @bspotfx
/*Insert into UserProduct_tbl values(@i,@userid,@bspotfx)*/
FETCH NEXT from cursorprofile
Into @userid,@bSpotFx
END
close cursorprofile
deallocate cursorprofile
fetch next from cursorproduct
into @sproductname
select @i=@i+1
end

close cursorproduct
"

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2001-12-27 : 13:51:56
To do this type of select select userid,('+ @sproductName +') from userprofile_tbl... you would have to use dynamic SQL. Unfortunately dynamic SQL does not blend well with cursors. For that matter, nothing in SQL blends well with cursors. Why not ditch the cursors and use the INSERT...SELECT syntax instead? It'd be faster.

--------------------------------------------------------------
1000 Posts, Here I come! I wonder what my new title will be...
Go to Top of Page
   

- Advertisement -