Explain the scenario in which CURSOR should be used over a standalone SELECT statement?

DB2DatabaseBig Data Analytics

The standalone SELECT statement is generally used when we use a primary or an alternate key in the WHERE clause. Therefore, In this case we are certain that the standalone select statement will return only one row since the primary key cannot have a duplicate value (multiple rows).

If we want to query the database using a non-unique key which could return multiple rows from a DB2 table, we have to use a cursor to handle the multiple rows returned. We can access the cursor in a loop to read each row data one by one.

For example, if we want to extract all the orders which were placed on 2020-07-28 from the ORDERS table, we have to use a cursor as there can be multiple orders in a particular date.

Published on 14-Sep-2020 18:43:20