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动态SQL的运用总结
热度:146 发布时间:2016-05-05 15:11:42.0
相关解决方案
- 大神们开始觅错啦!org.hibernate.exception.SQLGrammarException: could not execute query
- 小白Could not execute JDBC batch update解决办法
- struts2中 super,super.execute();是什么意思,如何理解
- org.hibernate.exception.SQLGrammarException: could not execute query有关问题求解
- could not execute query 错误
- asp.net 中有没有相仿 execute "Response.Write 0" 这样的命令
- context.Server.Execute 施行跳转报错
- nhibernate [could not execute query]错误
- Could not execute CVTRES.EXE [跪求解决方案!]解决方法
- HttpServerUtility.Execute 的一个判断,该怎么解决
- EXECUTE 后的事务计数指出缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。原计数 = 0,当前计数 = 1解决方法
- EXCEL VBA有没有类似vbs execute方法,小弟我写了个vbs程序,移植到VBA中"Execute s " 这句报错
- startserver失败cannot execute file RUN_SYBASE解决方法
- COBOL里头能不能用动态SQL EXECUTE
- COBOL里面能不能用动态SQL EXECUTE,该怎么解决
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:该怎么处理
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:该如何解决
- Could not execute JDBC batch update异常如何解决啊
- 报错:Could not execute JDBC batch update
- MyEclipse+Hibernate连接MSSQL数据库总是could not execute query
- statement.execute(s);
- Statement execute 执行 返回值错误 问题
- 不能这样输出结果吗?response.write(conn.execute(sql))解决办法
- 不能这么输出结果吗?response.write(conn.execute(sql))
- conn.execute 在服务器上执行的有关问题
- 读取数据id=conn.execute("select id from table where id2=1")(0)解决方法
- toconn.execute 使用的有关问题
- conn.execute 和 rs.open 效率测试!解决办法
- execute,redirect的区别,该怎么解决
- php中出现“Call to a member function Execute() on a non-object”的有关问题