SQL SERVER中错误delete语句可能误删除所有记录

这个 (2009-05-07 09:09)  以前写过的 一个帖子,现转回这里,做个备份


1. 创建表a
create table a(
id int identity(1,1),
a int
)
declare @i int
set @i = 0
while @i<100
begin
insert a(a) select @i+1
set @i=@i+1
end

2.创建表b
create table b (
b int
)
insert b(b) values(1)

3.以下语句是一个错误语句,原意是删除表a中id存在于表b中b字段的记录。应该是
delete a where id in (select b from b)
但一不小心写错了,写成:delete a where id in (select id from b)

4.如果你认为应该有出错提示,那你就错了。你会发现执行成功,以及a表中所有的数据都被删除了。

可惜发到Microsoft Connect并没有人回应这个问题!





终于等到Microsoft的回复

Hi,
The behavior you are seeing is by design. In the correlated subquery, if you do not qualify column references we will first try to bind the columns to the FROM clause in the subquery and if that fails we will try to bind to the FROM clause in the outer query. This is also according to ANSI SQL specifications and works pretty much the same in other database systems too. You have to use table aliases to write queries that doesn't result in such ambiguous behavior. See the KB article below for more details.

http://support.microsoft.com/kb/298674

--
Umachandar, SQL Programmability Team

 由 Microsoft 在 2009/5/8 10:54 发送

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值