Skip to content

Instantly share code, notes, and snippets.

@andrelashley
Created March 7, 2017 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrelashley/b461b909030c8ca974a2cfdafd9cd6cd to your computer and use it in GitHub Desktop.
Save andrelashley/b461b909030c8ca974a2cfdafd9cd6cd to your computer and use it in GitHub Desktop.
An example using cursors in oracle
DECLARE
-- step1: declare the variables
v_product_id product.product_id%type;
v_product_name product.product_name%type;
v_product_list_price product.list_price%type;
-- step 2: declare the cursor
CURSOR v_product_cursor IS
SELECT product_id, product_name, list_price
FROM product
ORDER BY product_id;
BEGIN
-- step 3: open the cursor
OPEN v_product_cursor;
LOOP
-- step 4: fetch the rows from the cursor
FETCH v_product_cursor
INTO v_product_id, v_product_name, v_product_list_price;
EXIT WHEN v_product_cursor%NOTFOUND;
SYS.DBMS_OUTPUT.PUT_LINE('v_product_id = ' || v_product_id ||
', v_product_name = ' || v_product_name || ', v_list_price = '
|| v_product_list_price);
END LOOP;
-- step 5: close the cursor
CLOSE v_product_cursor;
END;
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment