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' 無效。

*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章