PL/SQL语言中的execute immediate 命令只能用于DDL语句的执行,对于DML和DQL语句却不能实际执行,虽然将DML和DQL语句写成execute immediate 命令的形式执行时不会报错。
下面的例子来证明此结论:
SQL>select seq_article.nextval from dual;
seq_article.nextval
------------------------
51538
SQL>select seq_article.currval from dual;
seq_article.currval
------------------------
51538SQL> begin
for ind in 1..(52243-51538) loop
execute immediate ' select seq_article.nextval from dual';
//该句执行后select seq_article.currval from dual;值不变,说明 execute immediate ' select seq_article.nextval from dual';未被执行(虽然未报错)
end loop ;end;
SQL>select seq_article.currval from dual;
seq_article.currval
------------------------
51538执行过上述PL/SQL结构体后,select seq_article.currval from dual;查看到seq_article.currval的值并未变化,故而说明 execute immediate ' select seq_article.nextval from dual';未被实际执行(虽然未报错)。