動態SQL番外篇

2022-08-31 09:57:06 字數 4006 閱讀 9409

動態(dynamic)sql

1.區分靜態sql和動態sql

1)靜態sql

靜態sql指直接嵌入在pl/sql塊中的sql語句,靜態sql用於完成特定或固定的任務。

select sal from emp where empno=4000;

2)動態sql

動態sql執行pl/sql塊時動態輸入的sql語句。如果在pl/sql需要執行ddl語句,dcl語句,或者需要執行更加靈活的sql語句(select中有不同的where條件),需要用到動態sql。

編譯動態sql語句時,需要將sql語句存放到字串變數中,而且sql語句可以包含佔位符(以冒號開始)。

delete from emp where empno=:v_empno;

注意:能用靜態sql的一般不推薦使用動態sql,靜態sql的語句效能較優。

2.用動態sql處理非查詢語句

3.使用動態sql處理多行查詢語句

4.用集合處理動態sql語句

5.三種不同的動態sql方法

1)使用execute immediate語句

除不能處理多行查詢語句,其他的動態sql包括ddl語句,dcl語句以及單行的select查詢都可以。

2)使用open-for,fetch,close

能處理動態的多行查詢操作,必須使用open-for語句開啟游標,使用fetch語句迴圈提取資料,最終使用close語句關閉游標。

3)使用批量動態sql

通過使用批量動態sql語句,可以加快sql語句處理,進而提高pl/sql的效能。

6.execute immediate語法

execute immediate dynamic_string

[into ]

[using [in|out|in out] bind_argument]

[ into bind_argument...]

{}中內容是必須存在的。

1)使用execute immediate語句處理ddl操作

v=drop table ||v_table_name;

//刪除某個表

create or replace procedure pro_drop_table(v_table_name varchar2)

isv_sql varchar2(100);

begin

v_sql :=』drop table 『||v_table_name; —注意table後面的空格

execute immediate v_sql;

end;

2)處理dcl操作

//授予某個許可權給某個使用者

create or replace procedure pro_grant_priv(v_priv varchar2,

v_username varchar2)

isv_sql varchar2(100);

begin

v_sql := 『grant 『||v_priv||』 to 『||v_username;

execute immediate v_sql;

end;

##驗證

create user test identified by test;

exec pro_grant_priv(『create session』,'test』);

oracle>conn test/test;

3)處理dml操作

如果dml語句中包含佔位符,那麼execute immediate語句之後必須帶有using語句。如果dml語句中帶有returning子句,那麼在execute immediate語句之後需要帶有returning into子句

//給不同部門加薪

declare 

v_sql varchar2(100);

begin

v_sql := 『update emp set sal = sal*(1+:v_percent/100) where deptno=:v_deptno』;

execute immediate v_sql using &1,&2;

end;

7.使用open-for,fetch,close語句

##動態處理select語句返回多行資料

1)定義游標變數

type cursor_type is ref cursor;

cursor_variable cursor_type;

2)開啟游標變數

open cursor_variable for dynamic_string [using bind_argument...];

3)迴圈提取資料

fetch cursor_variable into ;

var提取標量變數,record_var提取記錄變數。

4)關閉游標

close cursor_variable;

//顯示指定部門的所有雇員名和工資

create or replace procedure pro_info(v_deptno number)

istype emp_cursor_type is ref cursor;

emp_cursor emp_cursor_type;

emp_record emp%rowtype;

v_sql varchar2(100);

begin

v_sql :=』select *  from emp where deptno=:v_deptno』;

open emp_cursor for v_sql using v_deptno;

loop

fetch emp_cursor into emp_record;

exit when emp_cursor%notfound;

dbms_output.put_line(『ename: 『||emp_record.ename ||』salary: 『||emp_record.sal);

end loop;

close emp_cursor;

end;

8.批量動態游標 –bulk

bulk加快批量資料的處理速度,使用bulk子句時,實際是動態sql語句將變數繫結為集合元素。

集合元素必須使用sql資料型別(char,number,varchar2,date,timestamp),不能使用pl/sql資料型別(binary_integer,boolean)。

1)動態bulk子句的語法:

execute immediate dynamic_string 

[bulk collect into define_variable]

[using bind_argument...]

[ bulk collect into return_variable...]

2)顯示特定部門的所有雇員名

set serveroutput on;

declare 

type ename_table_type is table of emp.ename%type index by binary_integer;

ename_table ename_table_type;

v_sql  varchar2(100);

begin 

v_sql:=』select ename from emp where deptno=:v_deptno』;

execute immediate v_sql

bulk collect into ename_table using &v_deptno;

for i in 1..ename_table.count loop

dbms_output.put_line(ename_table(i));

end loop;

end;

quexml

1.4.0 發布,web 問卷調查

genymotion

2.0 發布,安卓的 x86 模擬器

groovy&grails-**剪輯-日誌跟蹤

fossil

1.27 發布 分布式版本控制系統

應用之間呼叫

番外篇 日誌

一套能實現日誌輸出的工具包 日誌 能夠輸出系統執行的狀態,以及執行的時間 能力定製輸出目標 定製輸出格式 攜帶上下文 執行時選擇性輸出 靈活設定 效率高常見的日誌框架 來自這個包org.slf4.private logger logger logge ctory.getlogger user.cla...

前端開發 nginx番外篇

centos7下nginx開發使用 背景 阿里雲ecs centos7 安裝教程 centos7安裝nginx實戰 需要主意的如下 文中第四步 4.配置編譯引數命令 可以使用.configure help查詢詳細引數 如圖 執行。configure 啟動後檢視 4 檢視是否啟動 ps ef grep...

Mybatis系列番外篇之多引數

使用過mybatis的小夥伴們都知道,在對映檔案中只能使用parametertype屬性指定乙個傳入引數,可是在實際的專案中,往往需要用到多個傳入引數,那麼應該如何實現呢?本文就以mybatis介面式程式設計方式來分享一下我的實現方式。在實際的工作專案中,需要使用使用者和角色的概念對系統許可權進行管...