PL/SQL语言中的execute immediate 命令只能用于DDL语句的执行,对于DML和DQL语句却不能实际执行

时间:2021-06-13 22:31:20

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 

------------------------

51538
SQL>   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';未被实际执行(虽然未报错)。