Wednesday, October 25, 2017

simple cursor

Create table #tmp ( SalesOrderID int, OrderQty int ) GO --simple cursor in sql server Declare @orderid int, @orderqty int -- declare a cursor DECLARE insert_cursor CURSOR FOR SELECT SalesOrderId,OrderQty from Sales.SalesOrderDetail WHERE SalesOrderID=43659 -- open cursor and fetch first row into variables OPEN insert_cursor FETCH NEXT FROM insert_cursor into @orderid,@orderqty -- check for a new row WHILE @@FETCH_STATUS=0 BEGIN -- do complex operation here Insert into #tmp SELECT @orderid,@orderqty -- get next available row into variables FETCH NEXT FROM insert_cursor into @orderid,@orderqty END close insert_cursor Deallocate insert_cursor GO

No comments:

Post a Comment