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

简介: [20171231]PLSQL使用绑定变量.txt --//一些应用程序开发使用的绑定变量是:1,:2之列的语句,要调优这些语句必须写成如下: 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING              ...

[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 ,生成的脚本可能存在问题.要调整再执行.

目录
相关文章
|
SQL Oracle 关系型数据库
[20180511]PLSQL与fetchsize.txt
[20180511]PLSQL与fetchsize.txt --//测试看看PLSQL中cursor的fetchsize: 1.环境: SCOTT@book> @ ver1 PORT_STRING                    VERSION  ...
1162 0
|
SQL
[20180301]sql profile 非绑定变量.txt
[20180301]sql profile 非绑定变量.txt http://www.itpub.net/thread-2097379-1-1.html 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING            ...
856 0
|
关系型数据库 Oracle
|
SQL Perl 关系型数据库
[20171220]toad plsql显示整形的bug.txt
toad 显示 整形 异常
1332 0
[20171110]toad 小技巧.txt
[20171110]toad 小技巧.txt --//前几天的事情,一大早要求查询记录操作异常,本来想使用logminer查询,我们redo文件太大,使用logminer分析很慢.
992 0
|
SQL 测试技术 索引
[20161029]无法窥视在PLSQL.txt
[20161029]无法窥视在PLSQL.txt --测试使用PL/SQL无法窥视绑定变量的情况: --例子链接:https://connormcdonald.wordpress.
693 0
|
SQL Oracle 关系型数据库
[20160706]like % 绑定变量.txt
[20160706]like  % 绑定变量.txt --最近一直在优化一个项目,程序中存在大量的like模糊查询,例子: /* Formatted on 2016/7/6 11:10:55 (QP5 v5.
907 0
|
SQL 测试技术 索引
[20150610]sql的谓词中使用函数.txt
[20150610]sql的谓词中使用函数.txt --程序开发经常可以看到如下的代码: -- trunc(dd_date)=trunc(sysdate),结果导致必须建立函数索引。
962 0
|
Oracle 关系型数据库 索引
PLSQL_PLSQL Hint用法总结(概念)
2014-06-20 Created By BaoXinjian 一、摘要 手工指定SQL语句的执行计划 1. hints是oracle提供的一种机制,用来告诉优化器按照我们的告诉它的方式生成执行计划。
1125 0