I am trying to create a cursor that is uses dynamic SQL however the below procedure is erroring with "variable assignment is not allowed in a cursor declaration."
create procedure loadcode @tabname varchar(50) as DECLARE @tabload varchar(8000) DECLARE @client VARCHAR(10) DECLARE cur1 CURSOR FAST_FORWARD FOR SELECT @tabload = 'SELECT client FROM ' + @tabname + '__TEXT' OPEN cur1 FETCH NEXT FROM cur1 INTO @client WHILE @@FETCH_STATUS = 0 BEGIN PRINT + @client FETCH NEXT FROM cur1 INTO @client END CLOSE cur1 DEALLOCATE cur1 EXEC (@tabload)
I need to run an IF THEN ELSE insert into another table from the cursors output so how can I create this cursor? And if I was to use a WHILE loop how could it be coded?
Got it to work. For the record you need to declare the whole cursor from within dynamic sql.
answered Dec 03, 2009 at 07:31 AM
David 2 1