Friday, June 8, 2012

SQL - Cursor status function


-- Create a temp table
CREATE TABLE #TMP
(
   ii int
)
GO
-- Insert some testing data
INSERT INTO #TMP(ii) VALUES(1)
INSERT INTO #TMP(ii) VALUES(2)
INSERT INTO #TMP(ii) VALUES(3)

GO

--Create a cursor.
DECLARE cur CURSOR
FOR SELECT * FROM #TMP

--Display the status of the cursor before and after opening
--closing the cursor.

-- -1 Not open or closed
SELECT CURSOR_STATUS('global','cur') AS 'After declare'
OPEN cur
-- 1 Open
SELECT CURSOR_STATUS('global','cur') AS 'After Open'
CLOSE cur
-- -1 Not open or closed
SELECT CURSOR_STATUS('global','cur') AS 'After Close'

--Remove the cursor.
DEALLOCATE cur

--Drop the table.
DROP TABLE #TMP

No comments:

Post a Comment