8/08/2014

MS SQL Cursor Template

DECLARE x_cursor CURSOR FOR

-- add select here
SELECT name
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')

OPEN x_cursor
FETCH NEXT FROM x_cursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
       -- do something here

       FETCH NEXT FROM x_cursor INTO @name
END

CLOSE x_cursor
DEALLOCATE x_cursor