SQL Server2005中IN运算的一个错误

 

/*
测试in运算容易出错的一种情况,就是在子查询中的查询列实际不存在,会返回所有数据
*/


create   table  t_1(uid  int )
create   table  t_2(id  int )
insert   into  t_1
select   1
union   all  
select   2
union   all
select   3
union   all  
select   4


insert   into  t_2
select   1
union   all  
select   2
union   all
select   7
union   all  
select   8

select   *   from  t_1  where  uid  in  ( select   [ uid ]   from  t_2  where  id  like   ' [0-9] ' )

drop   table  t_1,t_2



/*
从例子中能够看出,子查询中如果使用本来不存在的列,如果编译没报错的话,查询的结果是错误的,并且不会有任何提示。还有就是子查询中的这个列名,并不是随便写就行,要重现这个错误,需要这个列名在t_1表中存在。

当然,如果单独执行select [user_id] from t_2 where id like '[0-9]' ,就会报错:

消息207,级别16,状态1,第26 行
列名'user_id' 无效。

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值