| Lesson 8 | Building a Cursor Block |
| Objective | Describe how to Build a Cursor Block |
DECLARE pet_count NUMBER; pet_name VARCHAR2(20); cursor pets is SELECT PRODUCT_NAME, CURRENT_INVENTORY_COUNT FROM PRODUCT WHERE PRODUCT_NAME = 'Canary';
BEGIN
IF NOT pets%ISOPEN THEN
OPEN pets;
END IF;
END;
FETCH pets INTO pet_name, pet_count;
EXIT WHEN pets%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('NUMBER OF ' || pet_name || ' = ' || pet_count);
CLOSE pets; END;
FOR loop, an alternate way to write cursor blocks. The following section discusses how to build a cursor block by walking you through the steps.
PETSTORE as the user name, GREATPETS as the password, and MYDB as the host string and clicked the OK button to continue. v_prod_id of type number and declaring a cursor called get_product that selects the product ID from product table for the product Rabbit.
You typed the following text at the following prompts and pressed Enter:
SQL>DECLARE2>v_prod_id NUMBER;3>CURSOR get_product ISSELECT PRODUCT_ID FROM PRODUCTWHERE PRODUCT_NAME = 'Rabbit';
BEGINOPEN get_product;FETCH get_product INTO v_prod_id;PET_CARE_LOG at the following prompt, and pressed Enter:INSERT INTO PET_CARE_LOG(PRODUCT_ID, LOG_DATETIME, LOG_TEXT)VALUES (v_prod_id, to_date('2-SEP-99 05:30PM','dd-mon-yy hh:miPM'), 'Rabbits eat fresh grass only.');CLOSE get_product;COMMIT;END;/