Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Query SQL Server with IN (NULL) Not Working

$
0
0

When I define a "User-Defined Table Type", as:

CREATE TYPE [dbo].[BitType] AS TABLE( [bit] NULL )

I place 0 and null in this table-variable. Then I do this query:

SELECT something FROM theTable WHERE item IN @theBitTypeTable

Will only get item=0 not item is null

Simply put: SELECT something FROM theTable WHERE item IN (0, NULL) is not working (no error although) It has to be SELECT something FROM theTable WHERE item=0 OR item IS NULL

So, my question is, if I like to use User-Defined Table Type , but I also need to use NULL value. How can I perform the query correctly to get result include null item.

Thanks (btw, I use MS SQL Server 2008 R2)

The only valid comparison operations with NULL values are IS NULL or IS NOT NULL , others always return false (actually - Unknown, see the @Damien_The_Unbeliever's comment)

So, try the following

CREATE TYPE [dbo].[BitType] AS TABLE( [tinyint] NOT NULL ) GO declare @theBitTypeTable BitType insert @theBitTypeTable VALUES(0), (2 /* instead of NULL*/) SELECT something FROM theTable WHERE IsNull(cast(item as tinyint), 2) IN (select B from @theBitTypeTable)

Viewing all articles
Browse latest Browse all 3160

Trending Articles