Oracle 在PLSQL中返回两个列表
我正在尝试将3个varray/集合的列表返回到我的应用程序。但是我遇到了问题,我认为我要么没有正确地实施解决方案Oracle 在PLSQL中返回两个列表,oracle,plsql,Oracle,Plsql,我正在尝试将3个varray/集合的列表返回到我的应用程序。但是我遇到了问题,我认为我要么没有正确地实施解决方案 create or replace PROCEDURE "GENERATE_PEOPLE" ( -- In this example pi_string will be "This.is.a.test" pi_string IN VARCHAR2 , po_firstnames OUT VARRAY , po_lastnames
create or replace
PROCEDURE "GENERATE_PEOPLE"
(
-- In this example pi_string will be "This.is.a.test"
pi_string IN VARCHAR2 ,
po_firstnames OUT VARRAY ,
po_lastnames OUT VARRAY ,
po_descriptions OUT VARRAY ,
po_error_code OUT VARCHAR2 ,
po_error_message OUT VARCHAR2
)
IS
CURSOR people_cursor IS SELECT firstname, lastname, description FROM people;
BEGIN
FOR person_rec IN people_cursor
LOOP
-- This is where I am trying to return 3 collections of po_firstnames, po_lastnames, po_descriptions
-- The print statements below print out exactly what it is I am trying to return!
dbms_output.put_line('Firstname: ' || person_rec.firstname);
dbms_output.put_line('Lastname: ' || person_rec.lastname);
dbms_output.put_line('Description: ' || pi_string || person_rec.description);
-- This is where the values would be added to the list/array/collection
po_firstnames(num?) := person_rec.firstname;
po_lastnames(num?) := person_rec.lastname;
po_descriptions(num?) := pi_string || person_rec.description;
END LOOP;
RETURN;
END;
非常感谢您的帮助
甲骨文10g
我这样称呼它:
DECLARE
TYPE po_firstnames AS vc2_array;
TYPE po_lastnames AS vc2_array;
TYPE po_descriptions AS vc2_array;
po_error_code VARCHAR2(50);
po_error_message VARCHAR2(50);
BEGIN
GENERATE_PEOPLE
(
'This.is.a.test' ,
po_firstnames ,
po_lastnames ,
po_descriptions ,
po_error_code ,
po_error_message
);
END;
VARRAY不能直接用作参数或变量的类型。相反,您需要创建一个VARRAY类型,如下所示(例如): 然后: 通常我会使用TABLE而不是VARRAY,因为使用TABLE时,您不必指定元素的最大数量:
create type vc2_array as table of varchar2(4000);
然后可以在循环中按如下方式分配值:
num := num+1; -- num must be declared above and initialised to 0
po_firstnames(num) := person_rec.firstname;
po_lastnames(num) := person_rec.lastname;
po_descriptions(num) := pi_string || person_rec.description;
但是,这样做更有效:
create or replace
PROCEDURE "GENERATE_PEOPLE"
(
-- In this example pi_string will be "This.is.a.test"
pi_string IN VARCHAR2 ,
po_firstnames OUT vc2_array ,
po_lastnames OUT vc2_array ,
po_descriptions OUT vc2_array ,
po_error_code OUT VARCHAR2 ,
po_error_message OUT VARCHAR2
)
IS
BEGIN
SELECT firstname, lastname, pi_string||description
BULK COLLECT INTO po_firstnames, po_lastnames, po_descriptions
FROM people;
END;
VARRAY不能直接用作参数或变量的类型。相反,您需要创建一个VARRAY类型,如下所示(例如): 然后: 通常我会使用TABLE而不是VARRAY,因为使用TABLE时,您不必指定元素的最大数量:
create type vc2_array as table of varchar2(4000);
然后可以在循环中按如下方式分配值:
num := num+1; -- num must be declared above and initialised to 0
po_firstnames(num) := person_rec.firstname;
po_lastnames(num) := person_rec.lastname;
po_descriptions(num) := pi_string || person_rec.description;
但是,这样做更有效:
create or replace
PROCEDURE "GENERATE_PEOPLE"
(
-- In this example pi_string will be "This.is.a.test"
pi_string IN VARCHAR2 ,
po_firstnames OUT vc2_array ,
po_lastnames OUT vc2_array ,
po_descriptions OUT vc2_array ,
po_error_code OUT VARCHAR2 ,
po_error_message OUT VARCHAR2
)
IS
BEGIN
SELECT firstname, lastname, pi_string||description
BULK COLLECT INTO po_firstnames, po_lastnames, po_descriptions
FROM people;
END;
您的参数是VARCHAR类型,这是正确的代码吗?哦,不,只是编辑了它-它们应该是VARRAY,这解决了问题吗?不,我得到了错误“错误(5,35):PLS-00201:标识符“VARRAY”必须声明为“使用PostgreSQL的Oracle 9i是什么意思?您的参数是VARCHAR类型,这是正确的代码吗?哦,不,刚刚编辑了它-它们应该是VARRAY,这解决了问题吗?不,我得到了错误“错误(5,35):PLS-00201:标识符‘VARRAY’必须声明”你说的Oracle 9i和PostgreSQL是什么意思?谢谢Tony,当我向表中添加值时,我应该这样添加它吗:vc2_数组(1):=person_rec.firstname?还是有更好的办法?我不需要创建3个表来返回3个单独的列表吗?我得到一个错误,说错误(5,35):PLS-00201:必须声明标识符“vc2_数组”,你能发布完整的推荐解决方案吗?我想我错误地实现了代码我现在可以创建类型了,但是,我似乎无法使用vc2_数组(1)向表中添加数据:='这是一个测试';有什么想法吗?你可以使用
pou的名字(1):=
等等。你好,托尼,谢谢,我认为你的解决方案非常优雅。非常感谢你的帮助。不过,我在用新类型调用过程时遇到了问题-我也用调用代码编辑了我的原始帖子:调用错误,但知道用自定义类型调用它的方式吗?谢谢Tony,当我向表中添加值时,是否应该这样添加:vc2_数组(1):=person_rec.firstname?还是有更好的办法?我不需要创建3个表来返回3个单独的列表吗?我得到一个错误,说错误(5,35):PLS-00201:必须声明标识符“vc2_数组”,你能发布完整的推荐解决方案吗?我想我错误地实现了代码我现在可以创建类型了,但是,我似乎无法使用vc2_数组(1)向表中添加数据:='这是一个测试';有什么想法吗?你可以使用pou的名字(1):=
等等。你好,托尼,谢谢,我认为你的解决方案非常优雅。非常感谢你的帮助。不过,我在用新类型调用过程时遇到了问题——我也用调用代码编辑了我的原始帖子:调用错误,但你知道用自定义类型调用它应该用什么方式吗?