|
We can use cursors within stored procedures.
Cursors are used to make a loop within a set of values within the database
without returning to the client side thus decreasing the time duration
and increasing the effeciency.
If a select query can return one or more record sets, then, a stored procedure
can make use of cursors. This cursor will loop through the records and we can make
what ever manipulations is expected to be done there.
For this, we need to understand a few terms and syntax. A simple example is as
below.
DECLARE @AuthorID char(11)
DECLARE c1 CURSOR FOR
SELECT au_id
FROM authors
OPEN c1
FETCH NEXT FROM c1
INTO @AuthorID
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @AuthorID
FETCH NEXT FROM c1
INTO @AuthorID
END
CLOSE c1
DEALLOCATE c1
So the things to be noticed here are:
|