oracle預存程序、聲明變數、for迴圈

來源:互聯網
上載者:User

標籤:array   sele   compute   mount   標題   ext   實現   begin   概念   

oracle預存程序、聲明變數、for迴圈 

1、建立預存程序

create or replace procedure test(var_name_1 in type,var_name_2 out type) as

--聲明變數(變數名 變數類型)

begin

--預存程序的執行體

end test;

列印出輸入的時間資訊

E.g:

create or replace procedure test(workDate in Date) is

begin

dbms_output.putline('The input date is:'||to_date(workDate,'yyyy-mm-dd'));

end test;

2、變數賦值

變數名 := 值;

E.g:

create or replace procedure test(workDate in Date) is

x number(4,2);

 begin

 x := 1;

end test;

3、判斷語句:

if 比較式 then begin end; end if;

E.g

create or replace procedure test(x in number) is

begin

        if x >0 then

         begin

        x := 0 - x;

        end;

    end if;

    if x = 0 then

       begin

        x: = 1;

    end;

    end if;

end test;

4、For 迴圈

For ... in ... LOOP

--執行語句

end LOOP;

(1)迴圈遍曆遊標

create or replace procedure test() as

Cursor cursor is select name from student; name varchar(20);

begin

for name in cursor LOOP

begin

 dbms_output.putline(name); 

end;

end LOOP;

end test;

(2)迴圈遍曆數組

 create or replace procedure test(varArray in myPackage.TestArray) as

--(輸入參數varArray 是自訂的數群組類型,定義方式見標題6)

i number;

begin

i := 1;  --預存程序數組是起始位置是從1開始的,與java、C、C++等語言不同。因為在Oracle中本是沒有數組的概念的,數組其實就是一張

--表(Table),每個數組元素就是表中的一個記錄,所以遍曆數組時就相當於從表中的第一條記錄開始遍曆

for i in 1..varArray.count LOOP     

dbms_output.putline('The No.'|| i || 'record in varArray is:'||varArray(i));  

 end LOOP;

end test;

5、While 迴圈

while 條件陳述式 LOOP

begin

end;

end LOOP;

E.g

create or replace procedure test(i in number) as

begin

while i < 10 LOOP

begin   

 i:= i + 1;

end;

end LOOP;

 end test;

6、數組

首先明確一個概念:Oracle中本是沒有數組的概念的,數組其實就是一張表(Table),每個數組元素就是表中的一個記錄。

使用數組時,使用者可以使用Oracle已經定義好的數群組類型,或可根據自己的需要定義數群組類型。

(1)使用Oracle內建的數群組類型

x array; --使用時需要需要進行初始化

e.g:

create or replace procedure test(y out array) is

 x array; 

 begin

x := new array();

y := x;

end test;

(2)自訂的數群組類型 (自訂資料類型時,建議通過建立Package的方式實現,以便於管理)

E.g (自訂使用參見標題4.2) create or replace package myPackage is

  -- Public type declarations   type info is record(     name varchar(20),     y number);

  type TestArray is table of info index by binary_integer;   --此處聲明了一個TestArray的類型資料,其實其為一張儲存Info資料類型的Table而已,及TestArray 就是一張表,有兩個欄位,一個是

name,一個是y。需要注意的是此處使用了Index by binary_integer 編製該Table的索引項目,也可以不寫,直接寫成:type TestArray is

table of info,如果不寫的話使用數組時就需要進行初始化:varArray myPackage.TestArray; varArray := new myPackage.TestArray();

end TestArray;

7.遊標的使用

    Oracle中Cursor是非常有用的,用於遍曆暫存資料表中的查詢結果。其相關方法和屬性也很多,現僅就常用的用法做一二介紹:

(1)Cursor型遊標(不能用於參數傳遞)

create or replace procedure test() is  

cusor_1 Cursor is select std_name from student where  ...;  --Cursor的使用方式1   cursor_2 Cursor;

begin

select class_name into cursor_2 from class where ...;  --Cursor的使用方式2

可使用For x in cursor LOOP .... end LOOP; 來實現對Cursor的遍曆

end test;

(2)SYS_REFCURSOR型遊標,該遊標是Oracle以預先定義的遊標,可作出參數進行傳遞

create or replace procedure test(rsCursor out SYS_REFCURSOR) is

cursor SYS_REFCURSOR; name varhcar(20);

begin

OPEN cursor FOR select name from student where ... --SYS_REFCURSOR只能通過OPEN方法來開啟和賦值

LOOP

 fetch cursor into name   --SYS_REFCURSOR只能通過fetch into來開啟和遍曆 exit when cursor%NOTFOUND;              --SYS_REFCURSOR中可使用三個狀態屬性:                                         ---%NOTFOUND(未找到記錄資訊) %FOUND(找到記錄資訊)                                         ---%ROWCOUNT(然後當前遊標所指向的行位置)

 dbms_output.putline(name);

end LOOP;

rsCursor := cursor;

end test;

下面寫一個簡單的例子來對以上所說的預存程序的用法做一個應用:

現假設存在兩張表,一張是學產生績表(studnet),欄位為:stdId,math,article,language,music,sport,total,average,step                   一張是學生課外成績表(out_school),欄位為:stdId,parctice,comment

通過預存程序自動計算出每位學生的總成績和平均成績,同時,如果學生在課外課程中獲得的評價為A,就在總成績上加20分。

create or replace procedure autocomputer(step in number) is

rsCursor SYS_REFCURSOR;

commentArray myPackage.myArray;

math number;

article number;

language number;

music number;

sport number;

total number;

average number;

stdId varchar(30);

record myPackage.stdInfo;

i number;

begin

i := 1;

get_comment(commentArray); --調用名為get_comment()的預存程序擷取學生課外評分資訊

OPEN rsCursor for select stdId,math,article,language,music,sport from student t where t.step = step;

LOOP

fetch rsCursor into stdId,math,article,language,music,sport; exit when rsCursor%NOTFOUND;

total := math + article + language + music + sport;

for i in 1..commentArray.count LOOP

 record := commentArray(i);   

if stdId = record.stdId then 

 begin    

 if record.comment = &apos;A&apos; then    

  begin        

 total := total + 20;  

   go to next; --使用go to跳出for迴圈      

  end;    

end if; 

end; 

end if;

end LOOP;

<<continue>>  average := total / 5;

 update student t set t.total=total and t.average = average where t.stdId = stdId;

end LOOP;

end;

end autocomputer;

--取得學生評論資訊的預存程序

create or replace procedure get_comment(commentArray out myPackage.myArray) is

rs SYS_REFCURSOR;

record myPackage.stdInfo;

stdId varchar(30);

comment varchar(1);

i number;

begin

open rs for select stdId,comment from out_school

i := 1;

LOOP

 fetch rs into stdId,comment; exit when rs%NOTFOUND;

record.stdId := stdId;

 record.comment := comment;

recommentArray(i) := record;

i:=i + 1;

end LOOP;

end get_comment;

--定義數群組類型myArray

create or replace package myPackage is begin

type stdInfo is record(stdId varchar(30),comment varchar(1));

type myArray is table of stdInfo index by binary_integer;

end myPackage;

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/squirrelrao/archive/2008/07/11/2639571.aspx

應用:

oracle遊標,預存程序,for迴圈,loop合一

create or replace procedure libsys.add_money(money out number) ascursor cur is

select amount from libsys.money_record;

begin

money:=0;

for amounts in cur

loop

money:=money+amounts;

end loop;

end add_money;

oracle預存程序、聲明變數、for迴圈

聯繫我們

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