如何循环通过例如从查询创建的隐式游标?
以下是示例代码:
SERVEROUTPUT on;
DECLARE
TYPE ref_cursor IS REF CURSOR;
cur REF_CURSOR;
BEGIN
OPEN cur FOR 'SELECT i.item_no,
i.item_descr
FROM ITEMS i
WHERE i.item_no in (1,2,3)';
... loop statement to print all item descriptions?
END;
答案 0 :(得分:10)
以下是如何允许动态SQL。您可以根据需要在代码中构建查询字符串(适用于SQL注入的常见警告)。
DECLARE
TYPE ref_cursor IS REF CURSOR;
cur REF_CURSOR;
d_item_no items.item_no%TYPE;
d_item_descr items.item_descr%TYPE;
BEGIN
OPEN cur FOR 'SELECT i.item_no,
i.item_descr
FROM ITEMS i
WHERE i.item_no in (1,2,3)';
LOOP
FETCH cur INTO d_item_no, d_item_descr;
EXIT WHEN cur%NOTFOUND;
dbms_output.put_line( d_item_no||' '||d_item_descr );
END LOOP;
CLOSE cur;
END;
/
答案 1 :(得分:8)
我没有接受11g的更改,但这应该有效:
BEGIN
FOR cur IN (SELECT i.item_no,
i.item_descr
FROM ITEMS i
WHERE i.item_no in (1,2,3))
LOOP
DBMS_OUTPUT.PUT_LINE('Row: '|| cur.item_no ||' '|| cur.item_descr);
END LOOP;
END;