CLOSE
Example
Section titled “Example”DECLARE emp_cursor CURSOR FOR SELECT * FROM employees;BEGIN OPEN emp_cursor; CLOSE emp_cursor;END;Output
Section titled “Output”This script won’t generate a visible output, because the cursors don’t produce visible outputs in Oracle database.
Explanation
Section titled “Explanation”In the Example, a cursor emp_cursor is declared, which fetches all records from the employees table. The OPEN statement opens the cursor, making it ready to fetch the data. The CLOSE statement, which is the focus of this example, is used to close the cursor emp_cursor after the usage. Since a closed cursor cannot be referenced, the CLOSE statement is essential to free up the memory in the database environment.
Example
Section titled “Example”DECLARE @sales CURSOR;SET @sales = CURSOR FORSELECT * FROM SalesOPEN @sales--perform operationsCLOSE @salesOutput
Section titled “Output”Command(s) completed successfully.
Explanation
Section titled “Explanation”In the above SQL Server code snippet, a cursor named @sales is being declared and set to select all records from the Sales table. The OPEN statement initiates the cursor, and after executing required operations, the CLOSE statement is used. This closes the cursor and releases the current result set. However, it does not deallocate resources. A cursor must be closed before it can be reopened. The output shows that the commands have been carried out successfully.