Oracle學習(一)

來源:互聯網
上載者:User

標籤:blog   io   ar   for   div   sp   cti   on   c   

1、建立一個對象:

create or replace type sys_col_id as object(id number(38))

2、定義一個數組

create or replace type sys_tbl_ids as table of sys_col_id

 3、算出分隔字元的長度,用法:splitter_count(‘1,2,3,4‘,‘,‘),返回的是分隔字元的長度

function splitter_count(str in varchar2, delim in char) return int as    val pls_integer;  begin    val := length(replace(str, delim, delim || ‘ ‘));    return val - length(str);  end;

 4、根據傳入的字串 返回數組

function tokenize_to_sys_tbl_ids(str varchar2, delim char)    return sys_tbl_ids as    target     int;    i          int;    this_delim int;    last_delim int;    ids_table  sys_tbl_ids := sys_tbl_ids();  BEGIN    i          := 1;    last_delim := 0;    target     := splitter_count(str, delim);    while i <= target loop      this_delim := instr(str, delim, 1, i);      ids_table.extend();      ids_table(i) := sys_col_id(to_number(substr(str,                                                  last_delim + 1,                                                  this_delim - last_delim - 1)));      i := i + 1;      last_delim := this_delim;    end loop;      ids_table.extend();    ids_table(i) := sys_col_id(to_number(substr(str, last_delim + 1)));      return ids_table;  end;

 

5、預存程序返回遊標

procedure MatchingInvoice(v_Invoiceids  varchar2,                            v_MatchingIds out sys_refcursor) as    ids_table        sys_tbl_ids := sys_tbl_ids();    v_rownum         number;    v_sys_tbl_ids    sys_tbl_ids;    v_TotalTaxAmount number(19, 6);    v_Totalamount    number(19, 6);  begin    v_rownum         := 1;    v_TotalTaxAmount := 0;    v_Totalamount    := 0;    --把傳入的Id放入數組中    v_sys_tbl_ids := pack_cmn.tokenize_to_sys_tbl_ids(v_Invoiceids, ‘,‘);    for record_invoice in (Select a.invoiceid,                                  sum(b.taxamount) taxamount,                                  a.totalamount / 1.17 * 0.17 newtaxamount                             From fi_invoice a                             Join fi_invoicedetail b                               On a.invoiceid = b.invoiceid
--把數組封裝成暫存資料表 Join table(v_sys_tbl_ids) c On c.id = a.invoiceid group by a.invoiceid, a.totalamount) loop --先累加 v_TotalTaxAmount := v_TotalTaxAmount + record_invoice.taxamount; v_Totalamount := v_Totalamount + record_invoice.newtaxamount; ids_table.extend(); ids_table(v_rownum) := sys_col_id(record_invoice.invoiceid); v_rownum := v_rownum + 1; if abs(round(v_TotalTaxAmount - v_Totalamount, 2)) >= 0.05 then exit; end if; end loop; open v_MatchingIds for Select c.id From table(ids_table) c; end MatchingInvoice;

 

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.