Sql 通用选择不';不能使用位类型

Sql 通用选择不';不能使用位类型,sql,sql-server,generics,select,stored-procedures,Sql,Sql Server,Generics,Select,Stored Procedures,基于 我尝试在我的表的上创建一个Select ALTER PROCEDURE _Einrichtung_Select -- Parameters with default values @EinrichtungId AS int = NULL, @EinrichtungName AS nvarchar(50) = NULL, @IsKueche AS bit = NU

基于

我尝试在我的表的上创建一个Select

ALTER PROCEDURE _Einrichtung_Select

    -- Parameters with default values
        @EinrichtungId      AS int          = NULL,
        @EinrichtungName    AS nvarchar(50) = NULL,
        @IsKueche           AS bit          = NULL,
        @RefEinrichtungId   AS int          = NULL,
        @RefSpeiseplantypId AS int          = NULL

AS

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
        SET NOCOUNT ON;

    -- generic SELECT query
        SELECT  *
        FROM    Einrichtung
        WHERE   EinrichtungId       = ISNULL(@EinrichtungId,        EinrichtungId)
        AND     EinrichtungName     = ISNULL(@EinrichtungName,      EinrichtungName)
        AND     IsKueche            = ISNULL(@IsKueche,             IsKueche)
        AND     RefEinrichtungId    = ISNULL(@RefEinrichtungId,     RefEinrichtungId)
        AND     RefSpeiseplantypId  = ISNULL(@RefSpeiseplantypId,   RefSpeiseplantypId)

        ORDER BY EinrichtungName

    RETURN

但是我发现位类型有一个问题,比如你可以看到它应该返回4行,但它只返回3行,那么我遗漏了什么呢?

这是因为你可以将
null
作为列的值。和SQL,因此检查
null=null
将返回
UNKNOWN
,而不是
TRUE
(如您所料)。 我认为这个查询将帮助您:

select *
from myTable
where
    (@EinrichtungId is null or EinrichtungId = @EinrichtungId) and
    (@EinrichtungName is null or EinrichtungName = @EinrichtungName) and
    (@IsKueche is null or IsKueche = @IsKueche) and
    (@RefEinrichtungId is null or RefEinrichtungId = @RefEinrichtungId) and
    (@RefSpeiseplantypId is null or RefSpeiseplantypId = @RefSpeiseplantypId)

啊,你是对的,我想(null==null)=>是的,现在它像我预期的那样工作:)谢谢