簡體   English   中英

Oracle SQL存儲過程返回集

[英]Oracle SQL Stored Procedure Returning Set

我認為這很簡單。

我有一個表People其中包含NameSurnameAge列。

我想要一個存儲過程GetPersonsBySurname

  • 輸入->一組姓
  • 輸出->該組姓氏中人的姓名,姓氏和年齡。

先感謝您。

現在我有了這個...我只能寫一個姓氏的存儲過程...我想要一組姓氏...

這是我的代碼

CREATE TABLE "PEOPLE" 
(
    "NAME" VARCHAR2(255 BYTE), 
    "SURNAME" VARCHAR2(255 BYTE), 
    "AGE" NUMBER(*,0)
)

標頭

create or replace PACKAGE "MYPACK" IS 

TYPE r_output IS RECORD 
(     

    name varchar2(255), 
    surname  varchar2(255), 
    age varchar2(255)

);

TYPE CURS_R_OUTPUT IS REF CURSOR
  RETURN r_output;

PROCEDURE GetPersonsBySurname ( mysurname in varchar2,
                                    OUT_P_CUR                       OUT CURS_R_OUTPUT,
                                    OUT_ESITO                       OUT VARCHAR2,
                                    OUT_MESSAGGIO                   OUT VARCHAR2
);

END MYPACK;

身體

create or replace PACKAGE BODY "MYPACK" 
IS
PROCEDURE GetPersonsBySurname  ( mysurname in varchar2,
                                    OUT_P_CUR                       OUT CURS_R_OUTPUT,
                                    OUT_ESITO                       OUT VARCHAR2,
                                    OUT_MESSAGGIO                   OUT VARCHAR2
                                    )
AS     
BEGIN

  OPEN OUT_P_CUR FOR
      select p.name, p.surname, p.age
      from people p
      where p.surname = mysurname ;


 EXCEPTION
 WHEN NO_DATA_FOUND 
 THEN
     OUT_ESITO := 'OK';
     OUT_MESSAGGIO := 'No data found';

 WHEN OTHERS
 THEN
     OUT_ESITO := 'KO';
     OUT_MESSAGGIO := 'ERROR';

 END GetPersonsBySurname;

 END MYPACK;

聲明一個架構級別的集合類型:

create or replace type surnames_coll
as
table of varchar2(255 byte)
;

然后在您的包內實現此存儲過程:

procedure GetPersonsByMultipleSurnames
    ( mysurnames                    in surnames_coll
    , out_p_cur                     out curs_r_output
    , out_esito                     out varchar2
    , out_messaggio                 out varchar2 )
as
begin
    open out_p_cur for
      select p.name, p.surname, p.age
      from table(mysurnames) x
          join people p
              on p.surname = X.column_value
    ;
exception
    when others then
        out_esito := 'KO';
        out_messaggio := 'ERROR';
end GetPersonsByMultipleSurnames;

來自PL / SQL,而不是

mypack.GetPersonsBySurname(
    mysurname => 'Esposito',
    out_p_cur => my_p_cur,
    out_esito => my_esito,
    out_messaggio => my_out_messaggio
);

你要打電話

mypack.GetPersonsByMultipleSurnames(
    mysurnames => surnames_coll('Esposito','Grande','con Carne','...'),
    out_p_cur => my_p_cur,
    out_esito => my_esito,
    out_messaggio => my_out_messaggio
);

當然,輸出ref游標建議您不要從PL / SQL調用它,而是從Java或.NET或類似的東西調用它。 在這種情況下,存儲過程可能會有所不同。

一個測試:

假設您在people表中插入了以下數據:

insert into people (name, surname, age) values ('Juan', 'Esposito', 1);
insert into people (name, surname, age) values ('Pablo', 'Esposito', 2);
insert into people (name, surname, age) values ('Maria', 'con Carne', 3);
insert into people (name, surname, age) values ('Chili', 'con Carne', 4);
insert into people (name, surname, age) values ('Ernesto', 'Grande', 5);
insert into people (name, surname, age) values ('Pedro', 'Grande', 6);
insert into people (name, surname, age) values ('Che', 'del Puerto', 5);
insert into people (name, surname, age) values ('Rita', 'del Puerto', 6);

然后在SQL * Plus中使用Espositocon CarneGrande姓氏進行簡單測試

SQL> var l_people_cursor refcursor;
SQL> var l_esito varchar2(32);
SQL> var l_messagio varchar2(4000);
SQL> 
SQL> begin
  2          mypack.GetPersonsByMultipleSurnames(
  3          mysurnames => surnames_coll('Esposito','Grande','con Carne', '...'),
  4          out_p_cur => :l_people_cursor,
  5          out_esito => :l_esito,
  6          out_messaggio => :l_messagio
  7          );
  8  end;
  9  /

PL/SQL procedure successfully completed.

SQL> 
SQL> print l_esito;

L_ESITO                                                                         
--------------------------------                                                


SQL> print l_messagio;

L_MESSAGIO                                                                      
--------------------------------------------------------------------------------


SQL> 
SQL> col name format a10
SQL> col surname format a20
SQL> 
SQL> print l_people_cursor;

NAME       SURNAME                     AGE                                      
---------- -------------------- ----------                                      
Pablo      Esposito                      2                                      
Juan       Esposito                      1                                      
Pedro      Grande                        6                                      
Ernesto    Grande                        5                                      
Chili      con Carne                     4                                      
Maria      con Carne                     3                                      

6 rows selected.

SQL> 

PS: exception when no_data_found then您在原始函數中使用exception when no_data_found then異常處理程序exception when no_data_found thenexception when no_data_found then完全無效。 打開游標將永遠不會引發此異常。

PS#2:out_messagio返回'ERROR'是非常不好的做法。 而是將行設置為out_messagio := sqlerrm;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM