SQL SERVER中错误delete语句可能误删除所有记录
2012-06-06 10:25
417 查看
这个(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 发送
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 发送
相关文章推荐
- mysql之Truncate 和Delete语句删除表的所有记录
- TRUNCATE TABLE 与 DELETE在删除整个表的所有记录时的区别具体到性能,效率,操作方式等方面
- SqlServer删除所有存储过程和所有表、查询表是否存在指定的记录及UPDATE语句
- Sql server 删除重复记录的SQL语句
- SQL SERVER快速删除数据库表里面的记录,truncate与delete的对比 【转】
- SQL Server 2008中SQL增强之三:Merge(在一条语句中使用Insert,Update,Delete) 一条语句实现两表同步(添加、删除、修改)
- Sql server 删除重复记录的SQL语句保留一条
- Sql Server自增列和delete truncate删除语句关系
- Sql server 删除重复记录的SQL语句
- TRUNCATE TABLE 与 DELETE在删除整个表的所有记录时的区别
- SQL SERVER 2005 获取表的所有索引信息以及删除和新建语句
- Sql server 删除重复记录的SQL语句
- 机房收费系统错误记录——sql server删除记录错误
- TRUNCATE TABLE 与 DELETE在删除整个表的所有记录时的区别
- sql2000 一次删除数据库表中所有记录的sql语句
- SQL SERVER 2005 获取表的所有索引信息以及删除和新建语句
- Sql server 删除重复记录的SQL语句
- HIve:beeline终端上在输错hive语句时,无论 Backspace还是delete 都删除不掉错误的语句,没有办法退格
- TRUNCATE TABLE 与 DELETE在删除整个表的所有记录时的区别
- TRUNCATE TABLE 与 DELETE在删除整个表的所有记录时的区别