首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

难道这是SQL Server的BUG ?解决办法

2012-03-25 
难道这是SQL Server的BUG ???/*写了个In条件的查询,手误写错了列名,发现竟然没报错! 百思不得其解,故来求

难道这是SQL Server的BUG ???
/*
写了个In条件的查询,手误写错了列名,发现竟然没报错! 百思不得其解,故来求救高手
具体请看代码

*/
--------------------

SQL code
drop table #acreate table #a ( a1 varchar(90), a2 int)insert #a select 'aaa1',1union all select 'aaa2',2union all select 'aaa2',2drop table #bcreate table #b (b1 varchar(90), b2 int)insert #b select 'aaa2-1',1union all select 'aaa2-2',2union all select 'aaa2-2',3union all select 'aaa2-3',3SELECT * FROM #b----   select b1 from #a   where a2=2   --直接执行这句会报错的,因为不存在b1列select * from #b where b1 in ( select b1 from #a   where a2=2 )    ---表#a中不存在b1列, 没报错,出现了所有数据select * from #b where b1 in (select b1 from #a   where  b2<3 )    ---表#a中不存在b1列,也不存在b2列,没报错,查出了#b表中b2<3的数据select * from #b where b1 in (select b1 from #a   where   a2=2 and b2=1)  --表#a中不存在b1和b2列,查出了#b表中b2=1的数据


/*-----环境具体信息-----------------------------------------------------
环境:Win7旗舰版SP1
SQL Server 2008 R2

Microsoft SQL Server Management Studio10.50.1600.1
Microsoft Analysis Services 客户端工具10.50.1600.1
Microsoft 数据访问组件 (MDAC)6.1.7601.17514
Microsoft MSXML3.0 4.0 6.0 
Microsoft Internet Explorer9.0.8112.16421
Microsoft .NET Framework2.0.50727.5448
操作系统6.1.7601
*/

[解决办法]
这个不是bug,in 后面的条件,当b1,b2 不存在于#a中时,只要存在于#b中一样不会报错,并识别为#b的b1,b2列。

如果你改成b3,b4 就该报错了,因为#a #b中都没有。
[解决办法]
应该是吧前面临时表的列拿过去IN查询所致
[解决办法]
这个没有官方出处。
但是很容易理解的。

SQL code
declare @a table (aid int,acol varchar(1))insert into @aselect 1,'a' union allselect 2,'b' union allselect 3,'c' union allselect 4,'d' union allselect 5,'e'declare @b table (bid int,bcol varchar(1))insert into @bselect 1,'a' union allselect 2,'b' union allselect 3,'c' select * from @a where aid>1 and aid in (select aid from @b)/*aid         acol----------- ----2           b3           c4           d5           e*/select * from @a where aid>1 and aid in (select bid from @b)/*aid         acol----------- ----2           b3           c*/ 

热点排行