[thelist] MSSQL Cursor help

Brian Cummiskey Brian at hondaswap.com
Thu Aug 18 22:54:53 CDT 2005

Joshua Olson wrote:

>declare DNCList cursor for
>	SELECT job AS 'joblist'
>	FROM jobs
>	WHERE --etc
>	FETCH NEXT FROM DNCList into @joblist
>		while @@fetch_status = 0
>		begin
>			set @query = 'SELECT * FROM ' + @joblist  --etc
>			exec(@query)
>			FETCH NEXT FROM DNCList into @joblist
>		end
Josh, et all -  thanks for the responses.  Fortunately, this was solved 
a couple days ago...  must've got clogged in the pipes.

What I ended up doing was creating a union statement, to union all the 
inner selects, and then finally exec() the created querystring.

The orig. code worked fine in query analizer, but since the produced out 
put was a series of selects (like writing:
 select * from table1
 select * from table2
and firing them off at the same time--  you get the two seperate results 
sets, thus, the recordset was only pulling the first select.
Unioned them all together, then fired it off, and things seem to work as 

But thanks.  perhaps moving the exec() inside is better than unioning 
them together.  I'll have to try this tomorrow at the office.

More information about the thelist mailing list