資料庫函數練習1__html

來源:互聯網
上載者:User

test表資料如下:

ikey      cname   pikey

1           集團       0

2           公司       1

3           部門       2

4           個人       3

試寫一遞迴函式,傳入ikey值,遍曆取出pikey值。運行時如傳入ikey為4,則該函數遍曆取出pikey值依次為3、2、1、0,最後返回0顯示到前台。

--建立表drop table test;drop sequence seq_test;create sequence seq_test;create table test( ikey number(5) primary key, cname varchar2(10), pikey number(3));insert into test values(seq_test.nextval,'集團',0);insert into test values(seq_test.nextval,'公司',1);insert into test values(seq_test.nextval,'部門',2);insert into test values(seq_test.nextval,'個人',3);commit;select * from test;--函數create or replace function func_test(i_int in number)return numberis  result number(3);  temp number(3);  type ref_cursor is ref cursor;  v_cursor ref_cursor;begin  open v_cursor for 'select pikey from test where ikey<= '||i_int||'order by ikey desc';  loop    fetch v_cursor into temp;    exit when v_cursor%notfound;    dbms_output.put_line(temp);    if(temp=0) then      result:=temp;      goto label1;    end if;  end loop;  <<label1>>  close v_cursor;  return result;  exception    when others then       if(v_cursor%isopen) then        close v_cursor;                          end if;      return '';end;--測試declare  v_pikey number;begin  v_pikey :=func_test(4);  dbms_output.put_line('結果值:'||v_pikey);end;



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.