生活随笔
收集整理的這篇文章主要介紹了
DBMS_SQL系统包的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉自:http://taosst.javaeye.com/blog/284770
PL/SQL中使用動態SQL編程?? ?? 在PL/SQL程序設計過程中,會遇到很多必須使用動態sql的地方,oracle系統所提供的DMBS_SQL包可以幫助你解決問題。?? (一)介紹?? DBMS_SQL 系統包提供了很多函數及過程,現在簡要闡述其中使用頻率較高的幾種:?? ?? function ?open_cursor:打開一個動態游標,并返回一個整型;?? ?? procedure ?close_cursor(c? in ? out ? integer );關閉一個動態游標,參數為open_cursor所打開的游標;?? ?? procedure ?parse(c? in ? integer ,?statement? in ?varchar2,?language_flag? in ? integer ):對動態游標所提供的sql語句進行解析,參數C表示游標,statement為sql語句,language-flag為解析sql語句所用oracle版本,一般有V6,V7跟native(在不明白所連 database 版本時,使用native);?? ?? procedure ?define_column(c? in ? integer ,?position? in ? integer ,? column ? any ?datatype,?[column_size? in ? integer ]):定義動態游標所能得到的對應值,其中c為動態游標,positon為對應動態sql中的位置(從1開始), column 為該值所對應的變量,可以為任何類型,column_size只有在 column 為定義長度的類型中使用如VARCHAR2, CHAR 等(該過程有很多種情況,此處只對一般使用到的類型進行表述);?? ?? function ? execute (c? in ? integer ):執行游標,并返回處理一個整型,1表示成功,0表示失敗,代表處理結果(對 insert , delete , update 才有意義,而對 select 語句而言可以忽略);?? ?? function ?fetch_rows(c? in ? integer ):對游標進行循環取數據,并返回一個整數,為0時表示已經取到游標末端;?? ?? procedure ?column_value(c? in ? integer ,?position? in ? integer ,?value):將所取得的游標數據賦值到相應的變量,c為游標,position為位置,value則為對應的變量;?? ?? procedure ?bind_variable(c? in ? integer ,? name ? in ?varchar2,?value):定義動態sql語句(DML)中所對應字段的值,c為游標, name 為字段名稱,value為字段的值;?? ?? 以上是在程序中經常使用到的幾個函數及過程,其他函數及過程請參照oracle所提供定義語句dbmssql.sql?? ?? (二)一般過程?? 對于一般的select 操作,如果使用動態的sql語句則需要進行以下幾個步驟:?? open ? cursor ?? 而對于dml操作(insert , update )則需要進行以下幾個步驟:?? open ? cursor ?? 對于delete 操作只需要進行以下幾個步驟:?? open ? cursor ?? ?? (三)實例應用?? 1.?declare ?? v_cid?integer ;?? v_updatestr?varchar2(100);?? v_rowupdated?integer ;?? begin ?? v_cid:=dbms_sql .open_cursor;?? v_updatestr:='update?emp?set?comm=400?where?empno=7499' ;?? dbms_sql .parse(v_cid,v_updatestr,dbms_sql .native);?? v_rowupdated:=dbms_sql .execute (v_cid);?? dbms_sql .close_cursor(v_cid);?? exception?? when ?others? then ?? dbms_sql .close_cursor(v_cid);?? raise;?? end ;?? 2.create ? or ? replace ? function ?updatecomm(p_comm?emp.comm%type,?p_empno?emp.empno%type?? return ? integer ? as ?? v_cid?integer ;?? v_updatestr?varchar2(100);?? v_rowupdated?integer ;?? begin ?? v_cid:=dbms_sql .open_cursor;?? v_updatestr:='update?emp?set?comm=:comm?where?empno=:empno' ;?? dbms_sql .parse(v_cid,v_updatestr,dbms_sql .native);?? dbms_sql .bind_variable(v_cid,'comm' , 'p_comm' );?? dbms_sql .bind_variable(v_cid,'empno' , 'p_empno' );?? v_rowupdated:=dbms_sql .execute (v_cid);?? dbms_sql .close_cursor(v_cid);?? return ?p_rowsupdated;?? exception?? when ?others? then ?? dbms_sql .close_cursor(v_cid);?? raise;?? end ;?? 調用 ?? declare ?? a?integer ;?? begin ?? a:=updatecomm(5000,a);?? dbms_output.put_line(a);?? end ;?? 3.create ? or ? replace ? procedure ?dynamiccopy(p_deptno1?emp.deptno%type? default ? null ,p_deptno2?emp.deptno%type? default ? null )?? as ?? v_cid?integer ;?? v_select?varchar2(100);?? v_empno?char (4);??? v_ename?varchar2(10);?? v_deptno?char (2);?? v_dummy?integer ;?? begin ?? v_cid:=dbms_sql .open_cursor;?? v_select:='select?empno,ename,deptno?from?emp?where?deptno?in(:d1,:d2)' ;?? dbms_sql .parse(v_cid,v_select,dbms_sql .native);?? dbms_sql .bind_variable(v_cid,'d1' ,p_deptno1);?? dbms_sql .bind_variable(v_cid,'d2' ,p_deptno2);?? dbms_sql .define_column(v_cid,1,v_empno,4);?? dbms_sql .define_column(v_cid,2,v_ename,10);?? dbms_sql .define_column(v_cid,3,v_deptno,2);?? v_dummy:=dbms_sql .execute (v_cid);?? loop?? if?dbms_sql .fetch_rows(v_cid)=0?then ?? exit;?? end ?if;?? dbms_sql .column_value(v_cid,1,v_empno);?? dbms_sql .column_value(v_cid,2,v_ename);?? dbms_sql .column_value(v_cid,3,v_deptno);?? insert ? into ?emp1(empno,ename,deptno)? values (v_empno,v_ename,v_deptno);?? end ?loop;?? dbms_sql .close_cursor(v_cid);?? commit ;?? exception?? when ?others? then ?? dbms_sql .close_cursor(v_cid);?? raise;?? end ;??? 4.DDL語句:DDL中聯編變量是非法的,即使在解析后不能夠調用bind_variable過程。另外,DDL解析后立即執行,不需要調用EXECUTE 過程,即使調用了也沒有用。?? create ? or ? replace ? procedure ?recreatetable(p_table? in ?varchar2,p_description? in ?varchar2)?? as ?? v_cursor?number;?? v_createstring?varchar2(100);?? v_dropstring?varchar2(100);?? begin ?? v_cursor:=dbms_sql .open_cursor;?? v_dropstring:='drop?table' ||p_table;?? begin ?? dbms_sql .parse(v_cursor,v_dropstring,dbms_sql .v7);?? exception?? when ?others? then ?? if?sqlcode!=-942?then ?? raise;?? end ?if;?? end ;?? v_createstring:='create?table' ||p_table||p_description;?? dbms_sql .parse(v_cursor,v_createstring,dbms_sql .native);?? dbms_sql .close_cursor(v_cursor);?? exception?? when ?others? then ?? dbms_sql .close_cursor(v_cursor);?? raise;?? end ;?
總結
以上是生活随笔 為你收集整理的DBMS_SQL系统包的使用 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。