Page 111 - CITS - Computer Software Application -TT
P. 111
COMPUTER SOFTWARE APPLICATION - CITS
2 Open Cursor Connection
Syntax:
OPEN cursor_connection
Query:
OPEN s
3 Close cursor connection
Syntax:
CLOSE cursor_name
Query:
CLOSE s1
4 Deallocate cursor memory
Syntax:
DEALLOCATE cursor_name
Query:
DEALLOCATE s1
How To Create an Implicit Cursor?
Syntax (for a Simple Cursor):
-- Cursor Declaration
DECLARE cursor_name CURSOR FOR
SELECT column1, column2
FROM table_name
WHERE condition;
-- Cursor Opening
OPEN cursor_name;
-- Cursor Fetching and Processing
FETCH NEXT FROM cursor_name INTO variable1, variable2;
-- Loop through the result set
WHILE @@FETCH_STATUS = 0
BEGIN
-- Process the current row (variable1, variable2)
-- Fetch the next row
FETCH NEXT FROM cursor_name INTO variable1, variable2;
END;
-- Cursor Closing
CLOSE cursor_name;
Example:
Suppose we want to process each employee’s name and salary from an employees table:
DECLARE emp_cursor CURSOR FOR
98
CITS : IT&ITES - Computer software application - Lesson 18 - 36