Oracle中函數/過程返回多個值(結果集)

來源:互聯網
上載者:User

標籤:

Oracle中函數/過程返回結果集的幾種方式:    

以函數return為例,預存程序只需改為out參數即可,在oracle 10g測試通過.    

(1) 返回遊標:

return的類型為:SYS_REFCURSOR

之後在IS裡面定義變數:curr SYS_REFCURSOR;

最後在函數體中寫:          open cur for             select ......;          return cur;        

例:        

CREATE OR REPLACE FUNCTION A_Test(orType varchar2)RETURN SYS_REFCURSOR is    type_cur SYS_REFCURSOR;BEGIN    OPEN type_cur FOR select col1,col2,col3 from testTable ;    RETURN  type_cur;END;

    (2)返回table類型的結果集:        

首先定義一個行類型:

CREATE OR REPLACE TYPE "SPLIT_ARR"  AS OBJECT(nowStr varchar2(18))

其次以此行類型定義一個表類型: 

CREATE OR REPLACE TYPE "SPLIT_TAB" AS TABLE of split_arr;

定義函數(此函數完成字串拆分功能): 

CREATE OR REPLACE FUNCTION GetSubStr(str       in varchar2, --待分割的字串                                                        splitchar in varchar2 --分割標誌                                                  ) return split_tab IS  restStr  varchar2(2000) default GetSubStr.str; --剩餘的字串                 thisStr  varchar2(18); --取得的當前字串                 indexStr int; --臨時存放分隔字元在字串中的位置                               v        split_tab := split_tab(); --返回結果              begin  dbms_output.put_line(restStr);  while length(restStr) != 0 LOOP    <<top>>    indexStr := instr(restStr, splitchar); --從子串中取分隔字元的第一個位置                           if indexStr = 0 and length(restStr) != 0 then      --在剩餘的串中找不到分隔字元                               begin        v.extend;        v(v.count) := split_arr(Reststr);        return v;      end;    end if;    if indexStr = 1 then      ---第一個字元便為分隔字元,此時去掉分隔字元                               begin        restStr := substr(restStr, 2);        goto top;      end;    end if;    if length(restStr) = 0 or restStr is null then      return v;    end if;    v.extend;    thisStr := substr(restStr, 1, indexStr - 1); --取得當前的字串                          restStr := substr(restStr, indexStr + 1); ---取剩餘的字串                           v(v.count) := split_arr(thisStr);  END LOOP;  return v;end;

 在PL/SQL developer中可以直接調用

cursor strcur is select nowStr from Table(GetSubStr(‘111,222,333,,,‘,‘,‘));

(3)以管道形式輸出: 

create type row_type as object(  a varchar2(10),  v varchar2(10)); --定義行對象         create type table_type as table of row_type; --定義表對象        create or replace function test_fun(a in varchar2, b in varchar2)return table_type pipelined is v row_type; --定義v為行物件類型         begin    for thisrow in (select a, b from mytable where col1 = a and col2 = b) loop         v := row_type(thisrow.a, thisrow.b);        pipe row(v);     end loop;     return;end; select * from table(test_fun(‘123‘, ‘456‘));

轉自 http://blog.csdn.net/feiliu010/article/details/1538822

Oracle中函數/過程返回多個值(結果集)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.