博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[20171231]PLSQL使用绑定变量.txt
阅读量:5996 次
发布时间:2019-06-20

本文共 3104 字,大约阅读时间需要 10 分钟。

[20171231]PLSQL使用绑定变量.txt

--//一些应用程序开发使用的绑定变量是:1,:2之列的语句,要调优这些语句必须写成如下:

1.环境:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

--//例子,这些语句无法在sqlplus下直接执行:

select * from emp where deptno = :1 and ename =:2 and hiredate =:3

--//必须转化成如下:

$ cat d.txt
begin
execute immediate q'[
select * from emp where deptno = :1 and ename =:2 and hiredate =:3
]'
using 10,'MILLER','1982-01-23 00:00:00';
end;
/
--//换一句话如果要优化最简单的方式写成上面的形式,自己没事写一个简单的脚本,实现这个功能:

2.测试脚本:

--//在toad下执行如下:
select * from emp where deptno = :1 and ename =:2 and hiredate =:3;
--//带入的参数对应是using 10,'MILLER','1982-01-23 00:00:00'.
--//查询获得sql_id=cz0pwgqy3xumj.

SCOTT@book> @ &r/bind_cap cz0pwgqy3xumj ''

C200
------------------------------------------------------------------
select * from emp where deptno = :1 and ename =:2 and hiredate =:3

SQL_ID        CHILD_NUMBER WAS NAME                   POSITION MAX_LENGTH LAST_CAPTURED       DATATYPE_STRING VALUE_STRING

------------- ------------ --- -------------------- ---------- ---------- ------------------- --------------- ---------------------
cz0pwgqy3xumj            0 YES :1                            1         22 2018-01-02 09:19:24 NUMBER          10
                           YES :2                            2         32 2018-01-02 09:19:24 VARCHAR2(32)    MILLER
                           YES :3                            3          7 2018-01-02 09:19:24 DATE            1982/01/23 00:00:00

--//注:使用listagg,仅仅在11g下有效.
$ cat b5.sql
spool &&1..txt
set feedback off
set head off
set verify off

select 'alter session set current_schema='||PARSING_SCHEMA_NAME||';' txt from v$sqlarea where sql_id = '&&1';

select 'alter session set statistics_level=all;' from dual;
select '' from dual;

select 'begin' txt from dual

union all
select 'execute immediate q''[' txt from dual
union all
select to_char(sql_fulltext) txt from v$sqlarea where sql_id='&&1'
union all
select ']'' using' txt from dual
union all
SELECT LISTAGG ( DECODE ( DATATYPE_STRING ,'NUMBER', value_string
            ,'DATE', '''' || TO_CHAR ( TO_DATE (value_string, 'mm/dd/yy hh24:mi:ss') ,'yyyy/mm/dd hh24:mi:ss') || ''''
            ,'''' || value_string || '''') ,CHR (10) || ',') WITHIN GROUP (ORDER BY POSITION)
          txt
  FROM v$sql_bind_capture
WHERE     sql_id = '&&1'
       AND was_captured = 'YES'
       AND DUP_POSITION IS NULL
union all
select ';' txt from dual
union all
select 'end;' txt from dual
union all
select '/'  txt from dual;

select 'rollback;' txt from dual;

spool off

set feedback 6
set head on

--//执行如下:
SYSTEM@book> @ &r/b5.sql cz0pwgqy3xumj
alter session set current_schema=SCOTT;
alter session set statistics_level=all;

begin

execute immediate q'[
select * from emp where deptno = :1 and ename =:2 and hiredate =:3
]' using
10
,'MILLER'
,'1982/01/23 00:00:00'

;

end;
/
rollback;

--//检查cz0pwgqy3xumj.txt脚本看看,是否存在问题:

$ cat cz0pwgqy3xumj.txt
alter session set current_schema=SCOTT;
alter session set statistics_level=all;

begin

execute immediate q'[
select * from emp where deptno = :1 and ename =:2 and hiredate =:3
]' using
10
,'MILLER'
,'1982/01/23 00:00:00'

;

end;
/
rollback;

--//当然有时候如果参数cursor_sharing=force ,生成的脚本可能存在问题.要调整再执行.

转载地址:http://nchlx.baihongyu.com/

你可能感兴趣的文章
.NET平台下的微信SDK(Rabbit.WeiXin)开源发布
查看>>
简要介绍如何集成Vitamio安卓版SDK
查看>>
BZOJ1107 : [POI2007]驾驶考试egz
查看>>
div与span区别及用法
查看>>
Senparc.Weixin.MP SDK 微信公众平台开发教程(十六):AccessToken自动管理机制
查看>>
【转载】COM小结
查看>>
[android] AndroidManifest.xml - 【 manifest -> permission】
查看>>
20150728月度会议
查看>>
explain mysql性能优化
查看>>
NoSql---MongoDB基本操作
查看>>
【移动开发】WIFI热点通信(二)
查看>>
C#多线程编程实例 螺纹与窗口交互
查看>>
ByteBuffer用法小结
查看>>
使用linux mysql客户端建立表时遇到格式解析的问题
查看>>
find the mincost route(最小环,最短路,floyd)
查看>>
TCP 的那些事儿(上)
查看>>
Discuz!NT3.6与网站整合(操作用户信息)解决方案
查看>>
【今日推荐】10大流行的 Metro UI 风格的 Bootstrap 主题和模板
查看>>
Swift - 实现拨打电话
查看>>
在线代码编辑、保存与运行网址推荐
查看>>