what is cursor procedure

Answer Posted / manoj kaushik

/* Same as previous example, this time using a
** cursor. Each update commits as it is made.
*/
create procedure increase_price_cursor
as
declare @price money

/* declare a cursor for the select from titles */
declare curs cursor for
select price
from titles
for update of price

/* open the cursor */
open curs

/* fetch the first row */
fetch curs into @price

/* now loop, processing all the rows
** @@sqlstatus = 0 means successful fetch
** @@sqlstatus = 1 means error on previous fetch
** @@sqlstatus = 2 means end of result set reached
*/
while (@@sqlstatus != 2)
begin
/* check for errors */
if (@@sqlstatus = 1)
begin
print "Error in increase_price"
return
end

/* next adjust the price according to the
** criteria
*/
if @price > $60
select @price = @price * 1.05
else
if @price > $30 and @price <= $60
select @price = @price * 1.10
else
if @price <= $30
select @price = @price * 1.20

/* now, update the row */
update titles
set price = @price
where current of curs

/* fetch the next row */
fetch curs into @price
end

/* close the cursor and return */
close curs
return

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Are sql connections encrypted?

560


how to select first 5 records from a table? : Sql dba

509


Is crud a cuss word?

548


What is a parameter query?

626


Is it possible to update views?

542






How do you declare a variable in pl sql?

535


What is set serveroutput on?

644


what are the nonstandard string types? : Sql dba

610


What is rowtype?

568


how to show all tables with 'mysql'? : Sql dba

610


Explain the uses of control file.

617


How to order siblings in oracle hierarchy queries?

600


Where the sql database files are stored?

495


how would concatenate strings in mysql? : Sql dba

572


how to dump a table to a file with 'mysqldump'? : Sql dba

555