網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、概述
在一般的sql操作中,sql語(yǔ)句基本上都是固定的,如:?SELECT t.empno,t.ename FROM scott.emp t WHERE t.deptno = 20;?
但有的時(shí)候,從應(yīng)用的需要或程序的編寫(xiě)出發(fā),都可能需要用到動(dòng)態(tài)SQl,如:?
當(dāng) from 后的表 不確定時(shí),或者where 后的條件不確定時(shí),都需要用到動(dòng)態(tài)SQL。
使用execute immediate語(yǔ)句可以處理包括ddl(create、alter和drop)、DCL(grant、revoke)、DML(insert、update、delete)以及單行select語(yǔ)句。
execute immediate語(yǔ)句:
execute immediate dynamic_string [into {define_variable[,define_variable]…|record}] [using [in|out|in out] bind_argument[,[in|out|in out]bind_argument]…] [{returning|return} into bind_argument[, bind_argument]…]
- define_variable用于指定存放單行查詢結(jié)果的變量;
- using in bind_argument用于指定存放傳遞給動(dòng)態(tài)sql值的變量,即在dynamic中存在占位符時(shí)使用;
- using out bind_argument用于指定存放動(dòng)態(tài)sql返回值的變量。
二、執(zhí)行ddl、dcl語(yǔ)句
不能使用into和using子句。
begin execute immediate 'create table ma_org(org_codevarchar2(20),org_name varchar2(254))'; execute immediate 'drop table ma_org'; end;
語(yǔ)句
begin execute immediate 'grant insert on ma_org to scott' end;
三、處理dml語(yǔ)句
1、給動(dòng)態(tài)語(yǔ)句傳值(USING 子句)
如果dml語(yǔ)句包含占位符,那么在execute immediate語(yǔ)句之后必須要帶有using子句;
declare orgcode varchar2(10); orgname varchar2(254); begin orgcode := 1200; execute immediate 'select org_name fromma_org where org_code = :X' into orgname using orgcode; dbms_output.put_line(orgname); end;
2、從動(dòng)態(tài)語(yǔ)句檢索值(INTO子句)
3、動(dòng)態(tài)調(diào)用存儲(chǔ)過(guò)程
declare l_routin varchar2(100) := 'gen2161.get_rowcnt'; l_tblnam varchar2(20) := 'emp'; l_cnt number; l_status varchar2(200); begin execute immediate 'begin ' || l_routin || '(:2, :3, :4); end;' using in l_tblnam, out l_cnt, in out l_status; if l_status != 'OK' then dbms_output.put_line('error'); end if; end;
4、處理包含returing子句的DML語(yǔ)句
如果dml語(yǔ)句帶有returning子句,那么在execute immediate語(yǔ)句之后必須帶有returning into子句,并且此時(shí)只能處理作用的單行上的dml語(yǔ)句,如果dml語(yǔ)句作用在多行上,則必須使用bulk子句。
declare orgcode varchar2(10); orgname varchar2(254); rname varchar2(254); begin orgcode := '1200'; orgname := '天津市分行'; execute immediate 'update ma_org set org_name=:X where org_code = :Y returning org_name into :rname' using orgname, orgcode returning into rname; dbms_output.put_line(orgname); end;
5、在retuing into中使用bulk collect into
四、處理多行查詢
oracle通過(guò)使用bulk collect into子句處理動(dòng)態(tài)sql中的多行查詢可以加快處理速度,從而提高應(yīng)用程序的性能。當(dāng)使用bulk子句時(shí),集合類型可以是plsql所支持的索引表、嵌套表和varray,但集合元素必須使用sql數(shù)據(jù)類型。在oracle9i以后,有三種語(yǔ)句支持bulk子句,execute immediate,fetch和forall。
1、使用動(dòng)態(tài)游標(biāo)(游標(biāo)變量)處理多行查詢類動(dòng)態(tài)sql語(yǔ)句。
DECLARE TYPE ref_cur IS REF CURSOR; rc ref_cur; emprow emp%ROWTYPE; v_sql VARCHAR2(100):= 'select * from emp where deptno = :x'; --動(dòng)態(tài)執(zhí)行的SQL語(yǔ)句 BEGIN OPEN rc FOR v_sql USING 30; --打開(kāi)游標(biāo),綁定執(zhí)行的SQL語(yǔ)句,并傳遞參數(shù) LOOP FETCH rc INTO emprow; EXIT WHEN rc%NOTFOUND; dbms_output.put_line('name:'||emprow.ename||' sal:'||emprow.sal); END LOOP; CLOSE rc; END;
2、在execute immediate中使用bulk collect into
示例:
declare type org_table_type is table of ma_org%rowtype; org_table org_table_type; v_orgcode varchar2(20); begin v_orgcode := '%00%'; execute immediate 'select * from ma_org where org_code like:v_orgcode' bulk collect into org_table using v_orgcode; for i in 1..org_table.count loop dbms_output.put_line(org_table(i).org_code||','||org_table(i).org_name); end loop; end;
3、在forall語(yǔ)句中使用bulk collect into語(yǔ)句
示例:
declare type type_org_code is table of ma_org.org_code%type; type type_org_name is table of ma_org.org_name%type; v_orgcode type_org_code; v_orgname type_org_name; begin v_orgcode := type_org_code('1100','1200'); forall i in 1..v_orgcode.count execute immediate 'update ma_org set org_name = org_code||org_namewhere org_code = :p1 returning org_name into :p2' using v_orgcode(i) returning bulk collect into v_orgname; for i in v_orgname.first..v_orgname.last loop dbms_output.put_line(v_orgname(i)); end loop; end;
原文鏈接:https://www.cnblogs.com/springsnow/archive/2011/12/09/2282528.html
相關(guān)推薦
- 2023-01-19 C++?new與malloc和delete及free動(dòng)態(tài)內(nèi)存管理及區(qū)別介紹_C 語(yǔ)言
- 2022-07-27 Golang的strings.Split()踩坑記錄_Golang
- 2022-01-29 寶塔部署Yii框架多個(gè)商城項(xiàng)目,隊(duì)列問(wèn)題“服務(wù)測(cè)試失敗,請(qǐng)檢查服務(wù)是否正常運(yùn)行”
- 2023-12-24 http中的get和post方法的區(qū)別
- 2023-02-10 Docker不同網(wǎng)段下的容器互聯(lián)的實(shí)現(xiàn)_docker
- 2022-05-25 Entity?Framework?Core使用控制臺(tái)程序生成數(shù)據(jù)庫(kù)表_實(shí)用技巧
- 2023-03-28 python數(shù)組如何添加整行或整列_python
- 2022-12-01 Go語(yǔ)言中基本數(shù)據(jù)類型的相互轉(zhuǎn)換詳解_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支