PL/SQL存储过程编译报错求助:按课程代码查询讲师与课程信息
Hey there! Let's walk through fixing your PL/SQL procedure step by step. I spotted several syntax and logical issues that are causing those compilation errors—let's break them down and fix them one by one.
First, let's list the problems in your original code:
- Missing commas/semicolons in variable declarations: You declared
FNAME VARCHAR(256)followed immediately byLNAME VARCHAR(256)without a comma or semicolon. PL/SQL requires clear separation between variables. - Wrong assignment operator: PL/SQL uses
:=for assigning values, not=. Lines likeFNAME=FIRST_NAMEare invalid here. - No
FETCHto get cursor data: Your loop checks if the cursor has no data, but never actually retrieves rows from the cursor. Without aFETCH, you'll never access the query results, and the loop will either run infinitely or exit right away. - Misspelled
DBMS_OUTPUT: You wroteDBMS.OUTPUT(with a dot) instead of the correctDBMS_OUTPUT. - Parameter name conflict: Your
CODEparameter shares the same name as theTEACHES.CODEcolumn, which confuses the database about which one to reference. - Unnecessary
GROUP BY: You’re usingGROUP BYwithout any aggregate functions (likeCOUNT()orSUM()), which is redundant here and could distort your results.
Here's the corrected procedure:
CREATE OR REPLACE PROCEDURE WHO(p_code CHAR) IS fname VARCHAR(256); lname VARCHAR(256); title VARCHAR(256); CURSOR c1 IS SELECT a.first_name, a.last_name, s.name FROM academic a INNER JOIN teaches t ON a.staff# = t.lecturer INNER JOIN subject s ON t.code = s.code WHERE t.year < 2016 AND t.code = p_code; -- Use prefixed parameter to avoid column name conflict BEGIN OPEN c1; LOOP FETCH c1 INTO fname, lname, title; -- Retrieve cursor row into variables EXIT WHEN c1%NOTFOUND; -- Exit loop once no more data exists DBMS_OUTPUT.PUT_LINE(fname || ' ' || lname); DBMS_OUTPUT.PUT_LINE(title); DBMS_OUTPUT.PUT_LINE('-----------------------'); -- Optional: Add a separator for readability END LOOP; CLOSE c1; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No lecturers found for course code: ' || p_code); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END WHO; /
Key fixes explained:
- Clean variable declarations: Added semicolons after each variable and commas between them to follow PL/SQL syntax rules.
- Parameter renaming: Changed
CODEtop_codeto avoid conflicting with theTEACHES.CODEcolumn—this makes your query unambiguous. - Cursor
FETCH: AddedFETCH c1 INTO fname, lname, title;to pull each row from the cursor into your variables. TheEXIT WHENis placed right after the fetch to ensure we exit as soon as there's no more data to process. - Fixed
DBMS_OUTPUT: Corrected the spelling to the properDBMS_OUTPUTpackage name (no dot). - Removed
GROUP BY: Since you aren't using aggregate functions, this clause was unnecessary and could have grouped rows incorrectly. - Added exception handling: Added basic error handling to catch cases where no data is found or unexpected errors occur—this makes your procedure more robust and user-friendly.
How to run the procedure:
First, enable output in your SQL tool (like SQL Developer or SQL*Plus), then execute the procedure with your course code:
SET SERVEROUTPUT ON; EXECUTE WHO('CS101'); -- Replace 'CS101' with your actual course code
Or using an anonymous block:
SET SERVEROUTPUT ON; BEGIN WHO('CS101'); END; /
内容的提问来源于stack exchange,提问作者Wendy Chong
相关产品推荐
相关产品推荐

