首页 > 数据库 > Oracle > DDL语句parse阶段隐式执行代码
通常情况下,一条SQL语句经过parse,bind variables,execution和fetch,但对于DDL是不经过后面两个阶段的,这个现象是同事刚刚发现,查一下文档上面也确实提到了DDL经过parse就执行了:
PARSE Procedure
This procedure parses the given statement in the given cursor. All statements are parsed immediately. In addition, DDL statements are run immediately when parsed.
There are two versions of the PARSE procedure: one uses a VARCHAR2 statement as an argument, and the other uses a VARCHAR2S (table of VARCHAR2) as an argument.
下面是一个测试例子:
SQL> declare
2 cr integer;
3 begin
4 cr:=dbms_sql.open_cursor;
5 dbms_sql.parse(cr,'create table abc as select * from dba_objects',dbms_sql.native);
6 dbms_sql.close_cursor(cr);
7 end;
8 /
PL/SQL procedure successfully completed.
SQL> select count(1) from abc;
COUNT(1)
----------
29202
SQL> declare
2 cr integer;
3 begin
4 cr:=dbms_sql.open_cursor;
5 dbms_sql.parse(cr,'truncate table abc',dbms_sql.native);
6 dbms_sql.close_cursor(cr);
7 end;
8 /
PL/SQL procedure successfully completed.
SQL> select count(1) from abc;
COUNT(1)
----------
0
对于truncate如果不小心执行了parse,整个表的数据就丢失了!
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/45188/viewspace-1030983/ ,如需转载,请注明出处,否则将追究法律责任。