在包 returns 内调用过程出错
Calling procedure inside a package returns an error
我正在尝试创建一个包,其中包含在我的表中插入行的过程。
包头:
CREATE OR REPLACE PACKAGE pkg_developers
AS
PROCEDURE empty_tables;
PROCEDURE add_city(country IN CITIES.COUNTRY%type, zipcode IN CITIES.ZIPCODE%type, city IN CITIES.CITY%type);
END pkg_developers;
包体:
CREATE OR REPLACE PACKAGE BODY pkg_developers AS
PROCEDURE empty_tables
IS
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE cities CASCADE';
/* More truncate statements*/
DBMS_OUTPUT.put_line('Tables truncated successfully.');
END empty_tables;
PROCEDURE add_city(p_country IN cities.COUNTRY%type, p_zipcode IN cities.ZIPCODE%type, p_city IN cities.CITY%type)
IS
BEGIN
INSERT INTO CITIES(COUNTRY, ZIPCODE, city) VALUES(p_country, p_zipcode, p_city);
COMMIT;
END add_city;
END pkg_developers;
我如何执行程序:
BEGIN
pkg_developers.empty_tables();
pkg_developers.add_city('Country', '1111', 'City');
END;
我得到的错误:
[2022-04-16 11:41:48] [72000][4063]
[2022-04-16 11:41:48] ORA-04063: package body "PROJECT.PKG_DEVELOPERS" has errors
[2022-04-16 11:41:48] ORA-06508: PL/SQL: could not find program unit being called: "PROJECT.PKG_DEVELOPERS"
[2022-04-16 11:41:48] ORA-06512: at line 2
[2022-04-16 11:41:48] Position: 0
[2022-04-16 11:41:48] Summary: 1 of 1 statements executed, 1 failed in 15 ms (106 symbols in file)
我确实发现,如果我删除过程 add_city
的参数并对值进行硬编码,它确实有效,但我终究无法弄清楚参数有什么问题,我在网上看了例子,他们看起来和我一样。
我也试过调用完全相同的过程,但不是在包内,然后它也能正常工作。
阅读它说的是什么:
package body "PROJECT.PKG_DEVELOPERS" has errors
您不能调用无效包(好吧,您 可以 - 正如您已经做的那样 - 但您知道结果)。
那么,怎么了?乍一看,这个:
包装规格说:
PROCEDURE add_city (country IN CITIES.COUNTRY%type,
zipcode IN CITIES.ZIPCODE%type,
city IN CITIES.CITY%type);
包裹主体写着:
PROCEDURE add_city (p_country IN cities.COUNTRY%type,
p_zipcode IN cities.ZIPCODE%type,
p_city IN cities.CITY%type)
找出不同之处。如果不能,那么:在包体中,所有参数都有前缀 p_
(这是正确的,即比包规范中的声明更好)。无论您选择哪个选项(我建议带前缀的 owe),请在规范和正文中使用它。
我正在尝试创建一个包,其中包含在我的表中插入行的过程。
包头:
CREATE OR REPLACE PACKAGE pkg_developers
AS
PROCEDURE empty_tables;
PROCEDURE add_city(country IN CITIES.COUNTRY%type, zipcode IN CITIES.ZIPCODE%type, city IN CITIES.CITY%type);
END pkg_developers;
包体:
CREATE OR REPLACE PACKAGE BODY pkg_developers AS
PROCEDURE empty_tables
IS
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE cities CASCADE';
/* More truncate statements*/
DBMS_OUTPUT.put_line('Tables truncated successfully.');
END empty_tables;
PROCEDURE add_city(p_country IN cities.COUNTRY%type, p_zipcode IN cities.ZIPCODE%type, p_city IN cities.CITY%type)
IS
BEGIN
INSERT INTO CITIES(COUNTRY, ZIPCODE, city) VALUES(p_country, p_zipcode, p_city);
COMMIT;
END add_city;
END pkg_developers;
我如何执行程序:
BEGIN
pkg_developers.empty_tables();
pkg_developers.add_city('Country', '1111', 'City');
END;
我得到的错误:
[2022-04-16 11:41:48] [72000][4063]
[2022-04-16 11:41:48] ORA-04063: package body "PROJECT.PKG_DEVELOPERS" has errors
[2022-04-16 11:41:48] ORA-06508: PL/SQL: could not find program unit being called: "PROJECT.PKG_DEVELOPERS"
[2022-04-16 11:41:48] ORA-06512: at line 2
[2022-04-16 11:41:48] Position: 0
[2022-04-16 11:41:48] Summary: 1 of 1 statements executed, 1 failed in 15 ms (106 symbols in file)
我确实发现,如果我删除过程 add_city
的参数并对值进行硬编码,它确实有效,但我终究无法弄清楚参数有什么问题,我在网上看了例子,他们看起来和我一样。
我也试过调用完全相同的过程,但不是在包内,然后它也能正常工作。
阅读它说的是什么:
package body "PROJECT.PKG_DEVELOPERS" has errors
您不能调用无效包(好吧,您 可以 - 正如您已经做的那样 - 但您知道结果)。
那么,怎么了?乍一看,这个:
包装规格说:
PROCEDURE add_city (country IN CITIES.COUNTRY%type,
zipcode IN CITIES.ZIPCODE%type,
city IN CITIES.CITY%type);
包裹主体写着:
PROCEDURE add_city (p_country IN cities.COUNTRY%type,
p_zipcode IN cities.ZIPCODE%type,
p_city IN cities.CITY%type)
找出不同之处。如果不能,那么:在包体中,所有参数都有前缀 p_
(这是正确的,即比包规范中的声明更好)。无论您选择哪个选项(我建议带前缀的 owe),请在规范和正文中使用它。