Skip to content
Advertisement

PL SQL print with cursor

i have a table with a column for categories, date and price. Like this:

So there are (almost) always two dates per group with two different prices. I have a sql statement which picks the row with the closest date to the given date but i don’t know how to print them all with a cursor. So the output for 06.06.2019 should look like this:

So it only prints one categorie + the correct Price (from the correct date) but 10 times.

Advertisement

Answer

Of course it does, as you told it to. You’re looping 10 times, and – for every loop iteration – you open/close the same cursor and print values it fetches.

What you should do is to loop through cursor itself; moreover, as you put it, that should be two nested loops. Something like this (pseudocode to make it clearer):


Applied to your code:


Though, reading what you actually asked, perhaps you’d want to look at the following code which is rather simpler than yours:

Sample data:

Procedure:

Testing:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement