Friday, March 9, 2012

how assign value to cursor using sp_executesql procedure

What is error here when i declare cursor ?

declare curQueryVehicleHave cursor for
exec sp_executesql @.strQueryVehicleHave

@.strQueryVehicleHave this string contain a queryMaybe someone can offer a better alternative, but as far as I know you can't do it that way. You will need to place the results of the EXEC into a #temp table and then use the #temp table as the source of your cursor.

At the risk of looking like an idiot, this is my testing code:


DECLARE @.myQuery nvarchar(800)
SET @.myQuery = 'select div_code from division'
DECLARE @.div_code varchar(10)
CREATE Table #Temp (div_code varchar(10))

INSERT INTO #Temp (div_code) EXECUTE sp_executesql @.myQuery -- note that a table variable will not work here

DECLARE curQueryVehicleHave CURSOR FOR SELECT * FROM #TEMP

OPEN curQueryVehicleHave

FETCH NEXT FROM curQueryVehicleHave INTO @.div_code

WHILE @.@.FETCH_STATUS = 0
BEGIN
PRINT @.div_code
FETCH NEXT FROM curQueryVehicleHave INTO @.div_code
END

CLOSE curQueryVehicleHave
DEALLOCATE curQueryVehicleHave
DROP TABLE #Temp

Terri|||I hate this about SQL Server. I'd like to see a syntax like

insert into TableName (Columns, ...)
select Columns, ...
from
exec StoredProc

Please SQL Server people?

No comments:

Post a Comment