0% found this document useful (0 votes)
30 views

Cursor

A cursor is used to retrieve a subset of data from a database in a row-based operation. It uses DECLARE, OPEN, FETCH, and CLOSE statements to access data from a SELECT statement. A stored procedure stores SQL statements and functions in a database that are ready to execute when called, allowing parameters to be passed in and a value returned. Triggers automatically execute a stored procedure or function in response to INSERT, DELETE, or UPDATE statements on a table.

Uploaded by

Vikram Kamath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Cursor

A cursor is used to retrieve a subset of data from a database in a row-based operation. It uses DECLARE, OPEN, FETCH, and CLOSE statements to access data from a SELECT statement. A stored procedure stores SQL statements and functions in a database that are ready to execute when called, allowing parameters to be passed in and a value returned. Triggers automatically execute a stored procedure or function in response to INSERT, DELETE, or UPDATE statements on a table.

Uploaded by

Vikram Kamath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Cursor- A cursor is typically used to retrieve a subset of data from the database in a

row based operation.


Syntax:

DECLARE CURSOR_NAME CURSOR


FOR SELECT_STATEMENT
[FOR [READ ONLY |UPDATE {[ COLUMN_LIST ]}]

Accessing the Cursor:


. OPEN: Opens a defined cursor - OPEN CURSOR_NAME
. FETCH: Fetches rows from a cursor into a program variable -

FETCH NEXT FROM CURSOR_NAME [ INTO

FETCH_LIST ]

. CLOSE: Closes the cursor when operations against the cursor are complete
CLOSE CURSOR_NAME
DEALLOCATE CURSOR CURSOR_NAME

The syntax for Microsoft SQL Server is as follows:


BEGIN
DECLARE @custname VARCHAR(30);
DECLARE namecursor CURSOR FOR SELECT CUST_NAME FROM TBL_CUSTOMER;
OPEN namecursor;
FETCH NEXT FROM namecursor INTO @custname
WHILE (@@FETCH_STATUS<>-1)
BEGIN
IF (@@FETCH_STATUS<>-2)
BEGIN
-- Do something with the variable
END
FETCH NEXT FROM namecursor INTO @custname
END
CLOSE namecursor
DEALLOCATE namecursor
END;

Procedure: A stored procedure is a group of one or more SQL Statements or


functions that are stored in the database. It is compiled and ready to be executed
by the user.
Functions: Functions are called by procedures. When a function is called by a
procedure, parameters can be passed into a function like a procedure, a value is
computed, and then the value is passed back to the calling procedure for further
processing. Function returns a value
When a stored procedure is created, the various subprograms and functions that
compose the stored procedure are actually stored in the database. These stored
procedures are prepared and are immediately ready to execute when the user
invokes them.
Advantages: Stored procedures can call other procedures and functions. Stored
Procedures can be called by other types of programs. Response time is typically
better with stored procedures.
Disadvantages: Debugging becomes difficult. Limited functionality.
TRIGGER: A trigger is a form of a stored procedure that is executed when a specified
DML action is performed on a table. The trigger can be executed before or after an
INSERT, DELETE, or UPDATE statement.

Advantages/Disadvantages: Triggers, for the most part, are very good functions to
use; they can,
however cause more I/O overhead. Triggers should not be used when a stored
procedure or a program can accomplish the same results with less overhead.

You might also like