我有一个示例数据库,我需要编写一个存储过程来打印给定的配方(db包含的内容,包括成分和数量 - 所以我需要JOIN表)
CREATE OR REPLACE PROCEDURE proc_printrecipe (RecipeName IN Recipe.RecipeTitle%TYPE)
IS
TYPE rec_printrecipe IS RECORD
(
rec_IngName Ingredients.IngredientName%TYPE,
rec_Amount Recipe_Ingredients.Amount%TYPE,
rec_MeasureDesc Measurements.MeasurementDescription%TYPE,
)
CURSOR cur_printrecipe IS
SELECT i.ingredientName, ri.Amount, m.MeasurementDescription FROM Recipes r
JOIN Recipe_Ingredients ri ON r.RecipeID = ri.RecipeID
JOIN Ingredients i ON ri.IngredientID = i.IngredientID
JOIN Measurements m ON ri.MeasureAmountID = m.MeasurementDescription
WHERE r.RecipeTitle = RecipeName;
BEGIN
IF NOT cur_printrecipe%ISOPEN THEN
OPEN cur_printrecipe;
END IF;
LOOP
FETCH cur_printrecipe INTO rec_printrecipe;
EXIT WHEN cur_printrecipe%NOTFOUND;
dbms_output.put_line( rec_Amount || ' '
rec_MeasureDesc || ' of '
rec_IngName
);
END LOOP;
END;
/
-- CALLING THE STORED PROCEDURE
DECLARE
recipe recipes.RecipeTitle%TYPE := 'Irish Stew';
BEGIN
proc_printrecipe(recipe);
END;
但脚本输出(使用 Oracle SQL Developer 1.1.3 )给了我: 警告:执行完成并带有警告
PROCEDURE proc_printrecipe Compiled.
Error starting at line 33 in command:
DECLARE
recipe recipes.RecipeTitle%TYPE := 'Irish Stew';
BEGIN
proc_printrecipe(recipe);
END;
Error report:
ORA-06550: Line 4, Column 3:
PLS-00905: Object COOK.PROC_PRINTRECIPE is invalid
ORA-06550: Line 4, Column 3:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
我错过了什么?
答案 0 :(得分:1)
您可以在过程编译后添加show errors
以查看问题,至少在SQL * Plus和SQL Developer中是这样;或查询user_errors
视图:
select * from user_errors
where type = 'PROCEDURE'
and name = 'PROC_PRINTRECIPE';
但是一些明显的错误是你在记录声明结尾处有一个额外的逗号;你错过了一个分号;并且您需要声明该类型的实例:
TYPE rec_printrecipe_type IS RECORD
(
IngName Ingredients.IngredientName%TYPE,
Amount Recipe_Ingredients.Amount%TYPE,
MeasureDesc Measurements.MeasurementDescription%TYPE
);
rec_printrecipe rec_printrecipe_type;
您的光标打开检查是多余的,因为它无法在此时打开;但是你应该在程序结束时明确地关闭它。
您的dbms_output
调用直接引用字段而不指定它们所属的记录,并且连接不完整;所以你需要这样做:
dbms_output.put_line( rec_printrecipe.Amount || ' ' ||
rec_printrecipe.MeasureDesc || ' of ' ||
rec_printrecipe.IngName
);
您可以假设无论是谁,都可以看到dbms_output
缓冲区。希望你已经在做set serveroutput on
。
user_errors
可能还会出现其他问题。
SQL Developer现在已达到第4版,因此您可以考虑升级。例如,Codo的回答可能指的是比版本1.1.3更现代的东西。
答案 1 :(得分:1)
错误消息表明您的存储过程无效,即由于代码中的错误而无法成功编译。
然后,SQL Developer将显示程序中的错误。