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

What is a crud api?

491


which types of join is used in sql widely? : Sql dba

535


Does sqlite need a server?

535


What does pl sql developer do?

504


What is the non-clustered index in sql?

582






How can we make an if statement within a select statement?

529


How to create a menu in sqlplus or pl/sql?

602


Inline the values in PL/SQL, what does it mean.?

621


What are the three pl sql block types?

562


what is innodb? : Sql dba

569


What are the disadvantages of file system?

609


What is sql lookup?

503


What are the different ways to optimize a sql query?

481


What is the use of double ampersand (&&) in sql queries? Give an example

600


What is the difference between database trigger and stored procedure?

546