SET SERVEROUTPUT ON;
DECLARE
--第一步聲明變量v_product_id products.product_id%TYPE;
v_name products.name%TYPE;
v_price products.price%TYPE;
--第二步聲明游標
CURSOR cv_product_cursor IS
SELECT product_id,name,price
FROM products
ORDER BY product_id;
BEGIN
--第三步打開游標
OPEN cv_product_cursor;
LOOP
--第四步從游標中取出每一行的值,分別存放到不同的列變量中
FETCH cv_product_cursor
INTO v_product_id,v_name,v_price;
--exit the loop when there are no more rows,as indicated by
--the Boolean variable cv_product_cursor%NOTFOUND (=true when
--there are no more rows)
--cv_product_cursor%NOTFOUND值為true,則說明游標已經達到了末尾
--此時就退出循環
EXIT WHEN cv_product_cursor%NOTFOUND;
--use DBMS_OUTPUT.PUT_LINE() to display the variables
DBMS_OUTPUT.PUT_LINE(
'v_product_id='||v_product_id||',v_name='||v_name||',v_price='
||v_price
);
END LOOP;
--第五步關閉游標
CLOSE cv_product_cursor;
END;
/