USE Northwind
declare Order_cursor cursor for
SELECT OrderID from Orders
open Order_cursor
declare @OrderID as INT
fetch next from Order_cursor into @OrderID
while(@@fetch_status =0)
begin
PRINT 'OrderID' + STR(@OrderID)
declare @SQLString nvarchar(500)
set @SQLString =N'declare my_cursor cursor for select ProductID from [Order Details] WHERE ORDERID=' + STR(@OrderID)
exec sp_executesql @SQLString
open my_cursor
declare @ProductID as INT
fetch next from my_cursor into @ProductID
while(@@fetch_status=0)
begin
PRINT 'ProductID:' + STR(@ProductID)
fetch next from my_cursor into @ProductID
end
close my_cursor
deallocate my_cursor
fetch next from Order_cursor into @OrderID
End
close Order_cursor
deallocate Order_cursor