View Thread
 Print Thread
Simple Cursor example
admin
We all hate em, but often, they are quick and easy way of doing adhoc tasks - a simple cursor example:


DECLARE @Cursor CURSOR 
DECLARE @colA VARCHAR(100)

SET @Cursor = CURSOR FAST_FORWARD
FOR
SELECT * FROM tableName WHERE Something = something

-- Now Open & Loop Through the Cursor
OPEN @Cursor
FETCH NEXT FROM @Cursor
INTO @ColA

WHILE @@FETCH_STATUS = 0
BEGIN
   -- Loop throught the cursor

   FETCH NEXT FROM @Cursor
   INTO @ColA
END

--Clear And Tidy Up
CLOSE @Cursor
DEALLOCATE @Cursor

Jordon Pilling | Heavencore Administrator
 
Jump to Forum