我正在尝试创建一个包含四个函数的包 . 每个函数将添加一组数字并从总数中减去一个 . 我在使语法正确方面遇到了很多麻烦 . 下面的函数自己工作,我尝试在最后调用第一个函数 .
当我尝试创建包时,我得到一个错误,在第7行,“遇到符号”END“时期望以下之一:开始函数编译指示程序子类型类型当前游标删除存在于0.05秒之前”
在包体中,它表示“名称已被现有对象使用” . 我不明白,因为它必须在包的规范中声明,并且如果错误是已经存在名为functionbyfour的包,则创建或替换应解决此问题 .
最后,当我尝试在包中使用一个函数时,它会说“遇到符号”BEGIN“时需要以下其中一个:: = . (@%; not null range default character符号”;“代替“BEGIN”继续.ORA-06550:第5行,第43栏:PLS-00103:当遇到下列情况之一时遇到符号“FROM”: . (*%&= - ; </> at in是mod余数不是rem <>或!=或〜=> = <= <>和/或喜欢喜欢2喜欢|| multiset me“ .
我使用的是ORACLE EXPRESS 11g版,是PL / SQL的新手(4周) .
任何输入都非常感谢 .
CREATE OR REPLACE FUNCTION functionbyfour AS
FUNCTION functone( first number, second number) RETURN NUMBER ;
FUNCTION functtwo( first number, second number, third number) RETURN NUMBER ;
FUNCTION functthree(first number, second number, third number, fourth number) RETURN NUMBER ;
FUNCTION functfour( first number, second number, third number, fourth number,fifth number) RETURN NUMBER ;
END functionbyfour;
/
CREATE OR REPLACE PACKAGE functionbyfour AS
FUNCTION functone (first number, second number ) RETURN number AS total number;
BEGIN
total:=first + second – 1;
RETURN total;
DBMS_OUTPUT.PUT_LINE(total);
END functone;
FUNCTION functtwo (first number, second number, third number ) RETURN number AS total number;
BEGIN
total:=first + second + third – 1;
RETURN total;
DBMS_OUTPUT.PUT_LINE(total);
END functtwo;
FUNCTION functthree (first number, second number,third number, fourth number ) RETURN number AS total number;
BEGIN
total:=first + second + third + fourth – 1;
RETURN total;
DBMS_OUTPUT.PUT_LINE(total);
END functthree;
FUNCTION functfour (first number, second number, third number, fourth number, fifth number ) RETURN number AS total number;
BEGIN
total:=first + second + third + fourth + fifth – 1;
RETURN total;
DBMS_OUTPUT.PUT_LINE(total);
END functfour;
/
BEGIN
SELECT functionbyfour.functone(1,2) FROM DUAL;
END;
/
3 回答
您需要创建一个名为
FunctionByFour
的包(CREATE OR REPLACE PACKAGE
)然后是相应的包体(
CREATE OR REPLACE PACKAGE BODY
) . 你还需要一个END
作为包体(现在,你的代码在第四个函数的末尾结束)完成后,您可以使用该功能
如果要将
SELECT
语句放在PL / SQL块中,则需要声明一个局部变量并执行SELECT INTO
以使用函数的结果填充局部变量(您也可以只为结果分配局部变量)函数调用,无需使用SELECT
) .另外,请注意在
RETURN
语句之后放置DBMS_OUTPUT.PUT_LINE
是没有意义的 . 永远无法达到该代码 . 如果要将结果打印到DBMS_OUTPUT
缓冲区,则需要在RETURN
之前 .应该
应该
单词second是关键字,您不能将其用作参数名称
你需要一个
在你的END functfour之后结束包体
您的dbms_outputs将永远不会在返回后执行
你可以在一个函数中完成所有这些
您对独立程序和包感到困惑 .
CREATE FUNCTION只能用于创建独立的功能 . 你有什么应该是:
包装由两部分组成,规格和主体 . 规范是API的公共面,正文是实现 . 你所拥有的包(规格)是包体 . 所以改变第二块代码开始
至少你的程序结构正确 .
Oracle PL / SQL文档是在线的,全面的和免费的 . 我劝你读一读 . Find out more .