基于 this answer
我试着为我的 table 创建一个选择
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
但是我的位类型有问题example sqlfiddle就像您看到的那样,它应该返回 4 行,但它只返回 3 行,所以我错过了什么?
最佳答案
这是因为您可以将 null
作为列的值。而 SQL 有 three-value logic ,因此检查 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)
关于sql - 通用选择不适用于位类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19007151/