在 PLSQL 中返回两个列表

发布于 2024-12-02 15:03:34 字数 1689 浏览 4 评论 0原文

我正在尝试将 3 个 VARRAYS/COLLECTIONS 的列表返回到我的应用程序。但我遇到了麻烦,认为我要么错误地实施了解决方案,

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;

非常感谢任何帮助

Oracle 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;

I am trying to return a list of 3 VARRAYS/COLLECTIONS to my application. I am having trouble though, think I am either implementing the solution incorrectly

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;

Any help is greatly appreciated

Oracle 10g

And I am calling it as such:

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吐个泡泡 2024-12-09 15:03:34

VARRAY 不能直接用作参数或变量的类型。相反,您需要创建一个像这样的 VARRAY 类型(例如):

create type vc2_array as varray(100) of varchar2(4000);

那么:

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 
)

通常我会使用 TABLE 而不是 VARRAY,因为使用 TABLE 您不必指定最大元素数:

create type vc2_array as table of varchar2(4000);

然后可以像这样分配值this 在你的循环中:

  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 cannot be used directly as the type of a parameter or variable. Instead you need to create a TYPE that is a VARRAY like this (e.g.):

create type vc2_array as varray(100) of varchar2(4000);

then:

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 
)

Generally I would use TABLE rather than VARRAY, bacause with TABLE you do not have to specify a maximum number of elements:

create type vc2_array as table of varchar2(4000);

The values can then be assigned like this in your loop:

  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;

However it would be more efficient to do this:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文