Oracle 19c中创建fetch存储过程出现编译错误,无法排查原因求助
Hey there, let's break down why your procedure is failing to compile properly—this is a super common gotcha with reserved keywords!
The Root Cause
fetch is a reserved keyword in Oracle SQL (it’s used to retrieve rows from a cursor during data processing). Oracle doesn’t allow you to use reserved words as names for database objects like procedures, functions, or tables. That’s exactly what’s triggering the "Procedure created with compilation errors" message.
How to Confirm the Exact Error Details
Before fixing it, you can check the specific error message to verify this is the issue:
- Run this command right after getting the compilation error:
SHOW ERRORS PROCEDURE fetch; - Or query the
USER_ERRORSview directly:SELECT line, position, text FROM user_errors WHERE UPPER(name) = 'FETCH';
You’ll see an error like "PLS-00103: Encountered the symbol 'FETCH' when expecting one of the following..." which confirms the keyword conflict.
Corrected Procedure Code
Rename your procedure to a non-reserved word (like fetch_user_errors below) and re-run the creation script:
CREATE OR REPLACE PROCEDURE fetch_user_errors(data OUT SYS_REFCURSOR) AS BEGIN OPEN data FOR SELECT * FROM user_errors; END; /
Verify the Procedure Works
Once you run the corrected script, if you don’t get any compilation error messages, you can test it with a simple PL/SQL block:
SET SERVEROUTPUT ON; DECLARE v_error_cursor SYS_REFCURSOR; v_error_record user_errors%ROWTYPE; BEGIN fetch_user_errors(v_error_cursor); LOOP FETCH v_error_cursor INTO v_error_record; EXIT WHEN v_error_cursor%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Error at Line ' || v_error_record.line || ': ' || v_error_record.text); END LOOP; CLOSE v_error_cursor; END; /
内容的提问来源于stack exchange,提问作者LEARNER OLY ASKING

