SQL Server 2005資料庫IN運算的一個錯誤

iSQlServer發表於2009-03-16
這篇論壇文章(賽迪網技術社群)主要介紹了SQL Server 2005資料庫中,有關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' 無效。

*/

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-567503/,如需轉載,請註明出處,否則將追究法律責任。

相關文章