EXECUTE IMMEDIATE

EXECUTE IMMEDIATE 代替了以前Oracle8i中DBMS_SQL package包.
它解析并马上执行动态的SQL语句或非运行时创建的PL/SQL块.动态创建和执行SQL语句性能超前,EXECUTE IMMEDIATE的目标在于减小企业费用并获得较高的性能,较之以前它相当容易编码.尽管DBMS_SQL仍然可用,但是推荐使用EXECUTE IMMEDIATE,因为它获的收益在包之上。

-- 使用技巧

1. EXECUTE IMMEDIATE将不会提交一个DML事务执行,应该显式提交
如果通过EXECUTE IMMEDIATE处理DML命令,
那么在完成以前需要显式提交或者作为EXECUTE IMMEDIATE自己的一部分.
如果通过EXECUTE IMMEDIATE处理DDL命令,它提交所有以前改变的数据

2. 不支持返回多行的查询,这种交互将用临时表来存储记录(参照例子如下)或者用REF cursors.

3. 当执行SQL语句时,不要用分号,当执行PL/SQL块时,在其尾部用分号.

4. 在Oracle手册中,未详细覆盖这些功能。
下面的例子展示了所有用到Execute immediate的可能方面.希望能给你带来方便.

5. 对于Forms开发者,当在PL/SQL 8.0.6.3.版本中,Forms 6i不能使用此功能.

EXECUTE IMMEDIATE -- 用法例子

1. 在PL/SQL运行DDL语句

begin
   execute immediate ‘set role all‘;
end;

2. 给动态语句传值(USING 子句)

declare
   l_depnam varchar2(20) := ‘testing‘;
   l_loc     varchar2(10) := ‘Dubai‘;
   begin
   execute immediate ‘insert into dept values   (:1, :2, :3)‘
     using 50, l_depnam, l_loc;
   commit;
end;

3. 从动态语句检索值(INTO子句)

declare
   l_cnt     varchar2(20);
begin
   execute immediate ‘select count(1) from emp‘
     into l_cnt;
   dbms_output.put_line(l_cnt);
end;

4. 动态调用例程.例程中用到的绑定变量参数必须指定参数类型.
黓认为IN类型,其它类型必须显式指定

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;

5. 将返回值传递到PL/SQL记录类型;同样也可用%rowtype变量

declare
   type empdtlrec is record (empno   number(4),
                            ename   varchar2(20),
                            deptno   number(2));
   empdtl empdtlrec;
begin
   execute immediate ‘select empno, ename, deptno ‘ ||
                    ‘from emp where empno = 7934‘
     into empdtl;
end;

6. 传递并检索值.INTO子句用在USING子句前

declare
   l_dept     pls_integer := 20;
   l_nam      varchar2(20);
   l_loc      varchar2(20);
begin
   execute immediate ‘select dname, loc from dept where deptno = :1‘
     into l_nam, l_loc
     using l_dept ;
end;

7. 多行查询选项.对此选项用insert语句填充临时表,
用临时表进行进一步的处理,也可以用REF cursors纠正此缺憾.

declare
   l_sal    pls_integer := 2000;
begin
   execute immediate ‘insert into temp(empno, ename) ‘ ||
                    ‘           select empno, ename from emp ‘ ||
                    ‘           where   sal > :1‘
     using l_sal;
   commit;
end;

对于处理动态语句,EXECUTE IMMEDIATE 比以前可能用到的更容易并且更高效.
当意图执行动态语句时,适当地处理异常更加重要.应该关注于捕获所有可能的异常.

时间: 2024-11-05 06:29:46

EXECUTE IMMEDIATE的相关文章

Maven Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean Failed to delete access_log

I'm trying to run simple struts project using maven and tomcat. When I'm trying to exucute next goals:clean install tomcat7:run ,there is an error : [ERROR] Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean)

【Android-tips】 Unable to execute dex: Multiple dex files define 解决方法

唔,之前已经想过今后不动android,没想到还是因为比赛的原因重操旧业.android有很多问题是由于eclipse的不完善造成的,比如今天遇到的这个问题 Unable to execute dex: Multiple dex files define [2011-10-23 16:23:29 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/myapp/R$array; [2011-10-23 16:23:

携程apollo源码在idea中启动报错TransportException: Cannot execute request on any known server

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) at com.sun.jersey.api.client.filter.GZIPConte

fetch 报错 Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.

TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body. 在"窗口"上执行"取"失败:GET / get方法的请求不能有正文. GET requests can't have a request body, you can't make them have one. GET requests only retrieve data,

解决mysqldump备份报错: Couldn't execute 'SHOW FIELDS FROM Unknown error 1356

服务器环境: [[email protected] mysql]# cat /etc/redhat-releaseCentOS Linux release 7.3.1611 (Core) [[email protected] mysql]# uname -aLinux localhost.localdomain 3.10.0-514.21.1.el7.x86_64 #1 SMP Thu May 25 17:04:51 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

Maven异常_02_compile faile_Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project

异常:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project 原因:(1)可能给eclipse配了一个jre而不是jdk,(2)可能是jdk版本过低 解决:window->preferences->java->installed JREs ,发现配了一个假的jdk,它是一个jre........ 配上一个jdk后,问题就解决

STM32初学Keil4编译时出现 Error:Failed to execute 'BIN40/Armcc'

一种是在系统开始--运行里输入cmd,查看armcc状态.详情见推文: http://blog.csdn.net/hicui/article/details/7350805(笔记记录,请勿见怪) 都没有问题的话,那么可以尝试: 在keil里的菜单栏依次选择Project->Manage->Components,Environment andBooks(CEB)(或者直接点击工具栏的图标), 然后在CEB里选择Folders/Extensions标签页,在RealView Folder里选择ke

execute as login 切换上下文

作为DBA,可能经常需要帮助Developer排除有关权限的问题.要确认某个账号是不是已经拥有了某权限,DBA并不需要使用该账号的登录名和密码进行验证,只需使用execute as语句,将当前会话的上下文切换到指定的login(登录)或者user(用户),就可以验证该账号是否拥有某权限. execute as user='user_name' 该语句模拟的上下文是当前数据库中的user,模拟范围仅限于当前数据库,任何对该数据库以外的资源的访问尝试都会导致失败,不管该user是否拥有相应权限. e

tomcat The file is absent or does not have execute permission

[[email protected] bin]# ./startup.sh Cannot find ./catalina.sh The file is absent or does not have execute permission This file is needed to run this program [[email protected] bin]# ll -h startup.sh -rwxrwxrwx. 1 root root 1.9K Nov 3 2014 startup.s

execute方法有感

有一个method困扰了我一周之久:execute 我跟着<java web开发技术详解>这本书一步一步编程,结果卡在了this.execute(request,response); 编译器一直报错:cannot resolve method execute 于是我百度,谷歌,查java sdk. 就在准备砸电脑的时候,我发现这个execute方法是作者自己定义的,后面一页就有... 这件事告诉我们,相比于埋头拉车,抬头看路才是最重要的. bye~