DDL语句parse阶段隐式执行代码

本文通过测试案例展示了在Oracle数据库中,数据定义语言(DDL)语句在解析(PARSE)阶段即被执行的特点。这与数据操作语言(DML)语句不同,后者还需要经历bindvariables、execution和fetch等步骤。这一特性意味着DDL语句如创建表或截断表的操作,在仅仅进行解析时就会生效。

通常情况下,一条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,整个表的数据就丢失了!

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/45188/viewspace-1030983/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/45188/viewspace-1030983/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值