2011-03-02 67 views
1

我使用jdbc從SQL Server檢索數據,並且ANSI_NULLS處於關閉狀態。所以如果我運行SQL Server上的JDBC,executeQuery()無法找到結果null

select * from cj_log where evt = null 

我可以得到結果。

但是,當我把它在一份聲明中這樣

Statement st = DBConnection.getConnection().createStatement(); 
String sql = "select * from CJ_LOG where EVT=null"; 
ResultSet rs = st.executeQuery(sql); 

結果集是空的。這裏有什麼問題?

+2

當您使用'select * from CJ_LOG where EVT IS NULL'時會發生什麼? – 2011-03-02 23:07:54

+1

ANSI_NULLS是每個連接設置。如果'DBConnection.getConnection()'不能確保ANSI_NULLS爲OFF,那麼很可能ANSI_NULLS實際上是ON(默認值)。 – ig0774 2011-03-03 02:50:28

回答

2

你需要說

where EVT is null 

,沒有什麼是等於零,甚至可以爲null。

認爲它是這樣的:空表示「不知道」。

你問「是什麼我不知道等於什麼我不知道。」

的答案是「我不知道」。所以你沒有得到任何行。

+1

儘管對於SQL標準來說絕對正確,但如果如問題所示,SQL Server實際上正在使用ANSI_NULLs關閉,那麼'select * from CJ_LOG where EVT = NULL'實際上等價於'select * from CJ_LOG where EVT IS NULL'。請參閱[這裏](http://msdn.microsoft.com/en-us/library/ms188048.aspx)。 – ig0774 2011-03-03 02:39:52

+0

點。底線是「打開ANSI_NULLS並保持打開狀態」。 :-) – Ben 2011-03-03 12:11:01

+0

對不起,遲到的迴應。謝謝大家的幫助。 – Raistlin 2012-03-19 14:33:50