declare v_out varchar2(50); begin execute immediate ‘select p_guid from c_itcomp where rownum = 1 ‘ into v_out; dbms_output.put_line(v_out); end;
数据库版本必须> =oracle 8i
直接的select语句是没有用的,猜测因为select返回的是多行多列
可以执行DML,DDL等
如insert, update, create等
还可以绑定变量
如 STRSQL:=
‘UPDATE
TNAME
SET
COL1=:VARIABLE1
WHERE COLX=:VARIABLE2 ‘;
EXCUTE
IMMEDIATE
STRSQL
USING VALUE1,VALUE2;
本文转载自:http://blog.sina.com.cn/s/blog_5421dfd20100r29j.html
执行动态的SQL语句或非运行时创建的PL/SQL块.动态创建和执行SQL语句
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;