I read somewhere that 99% of time you don’t need to use a cursor.
But I can’t think of any other way beside using a cursor in this following situation.
Select t.flag
From Dual t;
Let’s say this return 4 rows of either 'Y' or 'N'. I want the procedure to trigger something if it finds 'Y'. I usually declare a cursor and loop until %NOTFOUND. Please tell me if there is a better way.
Also, if you have any idea, when is the best time to use a cursor?
EDIT: Instead of inserting the flags, what if I want to do “If 'Y' then trigger something”?
Your case definitely falls into the 99%.
You can easily do the conditional insert using
insert into ... select.... It’s just a matter or making aselectthat returns the result that you want to insert.If you want to insert one record for each
'Y'then use a query withwhere flag = 'Y'. If you only want to insert a single record depending on whether there are at least one'Y', then you can adddistinctto the query.A cursor is useful when you make something more complicated. I for example use a cursor when need to insert or update records in one table, and also for each record insert or update one or more records into several other tables.