1. 程式人生 > >Oracle遊標/異常/過程/函數

Oracle遊標/異常/過程/函數

bst body enter erro 備註 iso 位移 回調 log

1、基礎語法

http://692088846.iteye.com/blog/2017137 (%type、%rowtype、if\if else\if elseif else、while、do..while、遊標、異常、函數、過程)

1.1 聲明變量賦值並輸出

set serveroutput on --設置數據庫輸出,默認為關閉,每次重新打開窗口需要重新設置。 Declare result integer; --聲明變量【變量名 變量類型】 begin result:=10+3*4-20+5**2; --給變量賦值【:=】 dbms_output.put_line(‘運算結果是:‘||to_char(result)); end; -------------------------------------------------------------------------------------------------- dbms_output.put_line函數輸出只能是
字符串,因此利用to_char函數將數值型結果轉換為字符型。
運算的優先次序為NOT、AND和OR。 To_char:將其他類型數據轉換為字符型。 To_date:將其他類型數據轉換為日期型。 To_number:將其他類型數據轉換為數值型。

1.2 %type、%rowtype、select...into

-- %type 變量的類型和數據表中的字段的數據類型一致 -- %rowtype 變量的類型和數據表中的一行記錄數據類型一致 -- select ... into 變量 表中查詢數據並賦值,可以一次性給多個變量賦值
declare v_object CUSTOMER%Rowtype; begin select * into v_object from customer where rownum=1; dbms_output.put_line(v_object.Enterprise_Code ||‘,‘||v_object.CreateDate); end;

1.3 只可以增刪改

-- insert,update,delete,select都可以,create table,drop table不行。DPL,DML,和流程控制語句可以在pl/sql裏用但DDL語句不行,表定義語言不可以在plsql中改變 declare v_name student.name%type:=‘wang‘; --聲明變量並賦值 begin insert into student(id,name,age) values(2,v_name,26); --插入數據 end; -------------------------------------------- declare v_name student.name%type:=‘hexian‘; --更新數據 begin update student set name=v_name where id=1; end; begin update student set name=‘qinaide‘ where id=2; end; ------------------------------------------------- --------------

2、 PLSQL流程控制

--if判斷--- declare v_b boolean:=true; -- := 是賦值 begin if v_b then dbms_output.put_line(‘ok‘); end if; end; --if else判斷--- declare v_b boolean:=true; begin if v_b then dbms_output.put_line(‘ok‘); else dbms_output.put_line(‘false‘); end if; end; --if elsif else判斷-- declare v_name varchar2(20):=‘cheng‘; begin if v_name=‘0701‘ then dbms_output.put_line(‘0701‘); elsif v_name=‘cheng‘ then dbms_output.put_line(‘cheng‘); else dbms_output.put_line(‘false‘); end if; end; --loop循環,註意推出exit是推出循環,而不是推出整個代碼塊 -------------------------------------------------------- loop簡化寫法 declare v_i binary_integer :=0; begin loop exit when v_i>10; v_i :=v_i+1; dbms_output.put_line(‘hehe‘); end loop; dbms_output.put_line(‘over‘); end; while循環------------------------------------------------- declare v_i binary_integer:=0; begin while v_i<10 loop dbms_output.put_line(‘hello‘||v_i ); v_i:=v_i+1; end loop; dbms_output.put_line(‘over‘); end; for循環,註意不需要聲明變量----------------------------------------------- begin for v_i in 0..10 loop dbms_output.put_line(‘hello‘||v_i); end loop; dbms_output.put_line(‘over‘); end; ***************************************** PLSQL異常處理 ***************************************** 1、聲明異常 異常名 EXCEPTION; 2、拋出異常 RAISE 異常名 3、處理異常 拋出異常後的邏輯代碼不會被繼續執行 異常的定義使用 ――――――――――――――――――――――――――――――――――――― begin dbms_output.put_line(1/0); --1/0出現錯誤,拋出異常,輸出error exception when others then dbms_output.put_line(‘error‘); end; ---------------- declare e_myException exception; --聲明異常 begin dbms_output.put_line(‘hello‘); --raise拋出異常,用此關鍵字,拋出後轉到自定義的e_myException,執行其 --裏面的putline函數後,再跳到end處,結束PL/SQL塊,raise接下面的2句不會繼續執行。 raise e_myException; dbms_output.put_line(‘world‘); dbms_output.put_line(1/0); exception when e_myException then --執行異常 dbms_output.put_line(sqlcode); --當前會話執行狀態,錯誤編碼 dbms_output.put_line(sqlerrm); --當前錯誤信息 dbms_output.put_line(‘my error‘); when others then dbms_output.put_line(‘error‘); end; ============================================================================================================ ***************************************** PLSQL遊標 ***************************************** 備註:下面提到的遊標為靜態cursor,包括顯示和隱式。 遊標,從declare、open、fetch、close是一個完整的生命旅程。當然了一個這樣的遊標是可以被多次open進行使用的,顯式cursor是靜態cursor,她的作用域是全局的,但也必須明白,靜態cursor也只有pl/sql代碼才可以使用它。靜態遊標變量是在定義時就必須指定SQL語句。 cursor 遊標(結果集)用於提取多行數據,定義後不會有數據,使用後才有。一旦遊標被打開,就無法再次打開(可以先關閉,再打開)。 declare cursor c_student is select * from book; begin open c_student; close c_student; end; 第二種遊標的定義方式,用變量控制結果集的數量。 declare v_id binary_integer; cursor c_student is select * from book where id>v_id; begin v_id:=10; open c_student; close c_student; end; 第三種遊標的定義方式,帶參數的遊標,用的最多。 declare cursor c_student(v_id binary_integer) is select * from book where id>v_id; begin open c_student(10); close c_student; end; 遊標的使用,一定別忘了關遊標。 declare v_student book%rowtype; cursor c_student(v_id binary_integer) is select * from book where id>v_id; begin open c_student(10); fetch c_student into v_student; close c_student; dbms_output.put_line(v_student.name); end; 如何遍歷遊標fetch 遊標的屬性 %found,%notfound,%isopen,%rowcount。 %found:若前面的fetch語句返回一行數據,則%found返回true,如果對未打開的遊標使用則報ORA-1001異常。 %notfound,與%found行為相反。 %isopen,判斷遊標是否打開。 %rowcount:當前遊標的指針位移量,到目前位置遊標所檢索的數據行的個數,若未打開就引用,返回ORA-1001。 註: no_data_found和%notfound的用法是有區別的,小結如下 1)SELECT . . . INTO 語句觸發 no_data_found; 2)當一個顯式光標(靜態和動態)的 where 子句未找到時觸發 %notfound; 3)當UPDATE或DELETE 語句的where 子句未找到時觸發 sql%notfound; 4)在光標的提取(Fetch)循環中要用 %notfound 或%found 來確定循環的退出條件,不要用no_data_found。 下面是幾個實例: create table BOOK ( ID VARCHAR2(10) not null, BOOKNAME VARCHAR2(10) not null, PRICE VARCHAR2(10) not null, CID VARCHAR2(10) not null ); -- %rowcount是SQL的屬性表示影響了多少條記錄 ======insert=================================================================== create or replace procedure say_hello( i_name in varchar2, o_result_msg out varchar2 ) as v_price varchar2(100); e_myException exception; begin insert into book(id,bookname,price) values (1,2,3); --這裏會報錯 o_result_msg := ‘success‘; exception when others then rollback; --o_result_msg := substr(sqlerrm, 1, 200); --返回完整錯誤信息 o_result_msg := substr(sqlcode, 1, 200); --返回錯誤碼 end; ====update/delete====================================================================== create or replace procedure say_hello( i_name in varchar2, o_result_msg out varchar2 ) as v_price varchar2(100); e_myException exception; begin update book set price = ‘55‘ where bookname = i_name; delete from book where bookname = i_name; if sql%notfound then raise e_myException; end if; o_result_msg := ‘success‘; exception when e_myException then rollback; o_result_msg := ‘update or delete dail‘; end; ===select====================================================================== create or replace procedure say_hello( i_name in varchar2, o_result_msg out varchar2 ) as v_price varchar2(100); e_myException exception; begin select price into v_price from book where bookname = i_name; o_result_msg := ‘success‘; exception when no_data_found then rollback; o_result_msg := ‘select into dail‘; end; =============================================================================== loop方式遍歷遊標 declare v_bookname varchar2(100); cursor c_book(i_id number) is select bookname from book where id = i_id; begin Open c_book(i_id); Loop Fetch c_book into v_bookname; exit when c_student%notfound; update book set price = ‘33‘ where bookname = v_bookname; End Loop; Close c_book; end; 或 declare v_bookname varchar2(100); cursor c_book(i_id number) is select bookname from book where id = i_id; begin Open c_book(i_id); Fetch c_book into v_bookname; While c_book%Found Loop update book set price = ‘33‘ where bookname = v_bookname; Fetch c_book into v_bookname; End Loop; Close c_book; end; ============================================================================================ while循環遍歷遊標,註意,第一次遊標剛打開就fetch,%found為null,進不去循環 解決方法:while nvl(c_student%found,true) loop declare v_bookname varchar2(100); cursor c_book(i_id number) is select bookname from book where id = i_id; begin Open c_book(i_id); while nvl(c_book%found,true) --或這種寫法:while c_book%found is null or c_book%found loop Fetch c_book into v_bookname; update book set price = ‘33‘ where bookname = v_bookname; End Loop; Close c_book; end; =================================================================================================== for循環遍歷,最簡單,用的最多,不需要 聲明v_student,Open和Close遊標和fetch操作(不用打開遊標和關閉遊標,實現遍歷遊標最高效方式) declare cursor c_book(i_id number) is select bookname from book where id = i_id; begin for cur in c_book(i_id) --直接將入參i_id傳入cursor即可 loop update book set price = ‘53‘ where bookname = cur.bookname; end loop; end; ***************************************** Oracle存儲過程 ***************************************** 在談存儲過程書寫中的一些規則時,先看一下執行它的規則,在命令窗口執行存儲過程sp_get_product_prompt set serveroutput on var ret1 varchar2(200); var ret2 varchar2(200); exec sp_get_product_prompt(83,:ret1,:ret2); --或execute print ret1; print ret2; 或 set serveroutput on declare ret1 varchar2(200); ret2 varchar2(200); begin sp_get_product_prompt(83,ret1,ret2); dbms_output.put_line(ret1); dbms_output.put_line(ret2); end; 存儲過程入參,不論類型,缺省情況下值都為null,入參和出參不能有長度,其中關鍵字as可以替換成is,存儲過程中變量聲明在as和begin之間,同時,存儲過程中可以再調用其它的存儲過程,如果要保證存儲過程之間的事務處理不受影響,可以定義為自治事務。 create or replace procedure say_hello( v_name in varchar2, v_flag number, o_ret out number ) as begin if v_name is null and v_flag is null then --v_name和v_flag都等於null o_ret := 10; else o_ret := 100; end if; end; 對於入參為null情況下給予缺省值 create or replace procedure say_hello( i_name in varchar2, i_flag number, o_ret out number ) as v_name varchar2(100); begin if i_name is null then v_name := ‘0‘; else v_name := i_name; end if; insert into phone(..,wname..,) values(..,v_name,..); end; 或直接在insert語句中調用nvl函數賦缺省值 insert into phone(..,wname..,) values(..,nvl(v_name,‘ ‘),..); ----如果將‘ ‘寫成‘‘,則insert進來的v_name值還是為‘‘等價於null值 帶一個參數的存儲過程 輸入參數in,輸入參數不能進行:=賦值,但可以將它賦給as後面定義的變量; 輸入參數in,可以作為變量進行條件判斷; 默認不寫就是in; 存儲過程沒有重載,這個有參的say_hello會替代已經存在的無參say_hello。 create or replace procedure say_hello(v_name in varchar2) as begin --v_name:=‘a‘; --存儲過程入參v_name不能做為賦值目標 dbms_output.put_line(‘hello ‘||v_name); end; 存儲過程輸入參數作為變量進行條件判斷 create or replace procedure say_hello( i_opFlag in number ) as v_name varchar2(100); begin if i_opFlag = 1 then v_name :=‘0‘; else v_name :=‘haha‘; end if; dbms_output.put_line(‘hello ‘||v_name); end; 利用存儲過程中定義的變量對入參的空值處理: create or replace procedure say_hello( i_name in varchar2 ) as v_name varchar2(100); begin if i_name is null then v_name :=‘0‘; else v_name :=i_name;--將入賦值給定義變量 end if; dbms_output.put_line(‘hello ‘||v_name); end; 多個參數的存儲過程 create or replace procedure say_hello( v_first_name in varchar2, v_last_name in varchar2) as begin dbms_output.put_line(‘hello ‘||v_first_name||‘.‘||v_last_name); end; out輸出參數,用於利用存儲過程給一個或多個變量賦值,類似於返回值 create or replace procedure say_hello( v_name in varchar2, v_content out varchar2 ) begin v_content:=‘hello‘||v_name; end; 調用: declare v_con varchar2(200); v_in varchar2(20):=‘wang‘; begin say_hello(v_in,v_con); dbms_output.put_line(v_con); end; in out參數,既賦值又取值 create or replace procedure say_hello(v_name in out varchar2) as begin v_name:=‘hi ‘||v_name; end; 調用: declare v_inout varchar2(20):=‘wangsu‘; begin say_hello(v_inout); dbms_output.put_line(v_inout); end; 對存儲過程入參賦缺省值 create or replace procedure say_hello( v_name varchar2 default ‘susu‘, v_content varchar2 default ‘hello‘ ) as begin dbms_output.put_line(v_name||‘ ‘||v_content); end; 調用:(用指明形參名的方式調用更好) begin say_hello(); end; 或 begin say_hello(‘cheng‘); end; 或 begin say_hello(v_name=>‘cheng‘); end; ***************************************** PLSQL中的function ***************************************** FUNCTION和PROCEDURE的區別 1、函數有返回值,過程沒有 2、函數調用在一個表達式中,過程則是作為pl/sql程序的一個語句 過程和函數都以編譯後的形式存放在數據庫中,函數可以沒有參數也可以有多個參數並有一個返回值。過程 有零個或多個參數,沒有返回值。函數和過程都可以通過參數列表接收或返回零個或多個值,函數和過程的 主要區別不在於返回值,而在於他們的調用方式,過程是作為一個獨立執行語句調用的,函數以合法的表達 式的方式調用 create or replace function func(v_name in varchar2) return varchar2 is begin return(v_name||‘ hello‘); end; --調用: declare v_name varchar2(20); begin v_name:=func(‘cheng‘); dbms_output.put_line(v_name); end; 帶out參數的函數 create or replace function func( v_name in varchar2, v_content out varchar2 ) return varchar2 is begin v_content:=v_name||‘ hello‘; return v_content; end; 調用: declare v_name varchar2(20); v_name1 varchar2(20); begin v_name1:=func(‘susu‘,v_name);--返回v_name值 dbms_output.put_line(v_name1);--打印func結果 dbms_output.put_line(v_name);--打印v_name結果 end; 帶in out 參數的函數 create or replace function func( v_name in out varchar2) return varchar2 is begin v_name:=v_name||‘ hello‘; return ‘cheng‘; end; 調用: declare v_inout varchar2(20):=‘world‘; v_ret varchar2(20); begin v_ret:=func(v_inout);--返回調用v_inout值(作為出參) dbms_output.put_line(v_ret);--打印func結果 dbms_output.put_line(v_inout);--返回v_name結果 end;

Oracle遊標/異常/過程/函數