您的位置:首页 > 数据库

SQL Server2005中IN运算的一个错误

2008-04-25 19:50 197 查看



/**//*


测试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' 无效。




*/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐