How To Declare a Cursor with "DECLARE ... CURSOR" in MS SQL Server?
Submitted by: AdministratorIf you want to use a cursor to represent the result set of a query, you need to define a cursor name with a SELECT sub-statement using the "DECLARE ... CURSOR" statement using the following syntax format:
DECLARE cursor_name CURSOR FOR
SELECT ...;
Note that the DECLARE statement will not actually execute the SELECT sub-statement. It only attaches the SELECT sub-statement to the cursor.
A cursor name should be deallocated to free up server resources if the cursor is not needed any more.
The tutorial example below shows you how to declare and deallocate a cursor.
USE GlobalGuideLineDatabase;
GO
DECLARE ggl_cursor CURSOR FOR
SELECT * FROM ggl_links;
-- other statements
DEALLOCATE ggl_cursor;
GO
Submitted by: Administrator
DECLARE cursor_name CURSOR FOR
SELECT ...;
Note that the DECLARE statement will not actually execute the SELECT sub-statement. It only attaches the SELECT sub-statement to the cursor.
A cursor name should be deallocated to free up server resources if the cursor is not needed any more.
The tutorial example below shows you how to declare and deallocate a cursor.
USE GlobalGuideLineDatabase;
GO
DECLARE ggl_cursor CURSOR FOR
SELECT * FROM ggl_links;
-- other statements
DEALLOCATE ggl_cursor;
GO
Submitted by: Administrator
Read Online MS SQL Server Job Interview Questions And Answers
Top MS SQL Server Questions
☺ | How To Start SQL Server Browser Service? |
☺ | How To Execute the Cursor Queries with "OPEN" Statements? |
☺ | How To Convert a Numeric Expression from One Data Type to Another? |
☺ | How To Use Subqueries with the EXISTS Operators in MS SQL Server? |
☺ | How To Find Out What Is the Default Collation in a Database? |
Top Databases Programming Categories
☺ | RDBMS Interview Questions. |
☺ | SQL Interview Questions. |
☺ | SSRS Interview Questions. |
☺ | Database Administrator (DBA) Interview Questions. |
☺ | Sybase Interview Questions. |