我正在尝试迭代来自DBMS_SQL.TO_CURSOR_NUMBER的游标号,并遇到问题 - 当我尝试将值拉入变量时,我得到ORA-01007(变量不在选择列表中)。
这是一个复制我的问题的代码块:
DECLARE
cur SYS_REFCURSOR;
nm INTEGER;
colDescs DBMS_SQL.DESC_TAB2;
numCols INTEGER;
val VARCHAR2(3);
BEGIN
OPEN cur FOR
SELECT 'x' AS foo, 2 AS bar
FROM dual;
nm := DBMS_SQL.TO_CURSOR_NUMBER(cur);
DBMS_SQL.DESCRIBE_COLUMNS2(nm, numCols, colDescs);
DBMS_OUTPUT.PUT_LINE(numCols);
DBMS_OUTPUT.PUT_LINE(DBMS_SQL.FETCH_ROWS(nm));
DBMS_SQL.column_value(nm, 1, val);
DBMS_OUTPUT.PUT_LINE(val);
DBMS_SQL.CLOSE_CURSOR(nm);
EXCEPTION
WHEN OTHERS THEN
DBMS_SQL.CLOSE_CURSOR(nm);
DBMS_OUTPUT.PUT_LINE('borked '||SQLCODE);
END;
/
预期产出:
2
1
x
实际输出:
2
1
borked -1007
答案 0 :(得分:3)
您还没有完成DEFINE_COLUMN
步骤; before you fetch:
DBMS_SQL.DEFINE_COLUMN(nm, 1, val, 3);
这样可行:
DECLARE
cur SYS_REFCURSOR;
nm INTEGER;
colDescs DBMS_SQL.DESC_TAB2;
numCols INTEGER;
val VARCHAR2(3);
BEGIN
OPEN cur FOR
SELECT 'x' AS foo, 2 AS bar
FROM dual;
nm := DBMS_SQL.TO_CURSOR_NUMBER(cur);
DBMS_SQL.DESCRIBE_COLUMNS2(nm, numCols, colDescs);
DBMS_OUTPUT.PUT_LINE(numCols);
DBMS_SQL.DEFINE_COLUMN(nm, 1, val, 3);
DBMS_OUTPUT.PUT_LINE(DBMS_SQL.FETCH_ROWS(nm));
DBMS_SQL.column_value(nm, 1, val);
DBMS_OUTPUT.PUT_LINE(val);
DBMS_SQL.CLOSE_CURSOR(nm);
EXCEPTION
WHEN OTHERS THEN
DBMS_SQL.CLOSE_CURSOR(nm);
DBMS_OUTPUT.PUT_LINE('borked '||SQLERRM);
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
END;
/
anonymous block completed
2
1
x