我有一个我创建的存储过程,有时会有一些传入NULL值的参数。我设置了存储过程以考虑到这一点,但每次传入NULL值时,即使传入其他一些参数,它也不会返回任何内容。你能帮忙吗?这是我的存储过程。我不确定它是不正确的连接还是什么。
感谢。
@ProductID int = NULL,
@CollectionID int = NULL,
@ApplicationID int = NULL,
@StyleID int = NULL
AS
SELECT
dbo.CrossoverDesignTable.ColorNum, dbo.CrossoverDesignTable.CrossoverID,
dbo.ImagesWebsite.Description,
dbo.DesignNameTable.DesignDescription + ' ' + dbo.CrossoverDesignTable.ColorNum AS DesignColor,
dbo.CollectionTable.CollectionDescription
FROM
dbo.CrossoverDesignTable
INNER JOIN
dbo.DesignNameTable ON dbo.CrossoverDesignTable.DesignNameID = dbo.DesignNameTable.DesignNameID
INNER JOIN
dbo.ImagesWebsite ON dbo.CrossoverDesignTable.ProductImageID = dbo.ImagesWebsite.ProductImageID
INNER JOIN
dbo.CollectionTable ON dbo.CrossoverDesignTable.CollectionID = dbo.CollectionTable.CollectionID
WHERE
(dbo.CrossoverDesignTable.ProductID = @ProductID OR @ProductID IS NULL)
AND (dbo.CrossoverDesignTable.CollectionID = @CollectionID OR dbo.CrossoverDesignTable.CollectionID IS NULL)
AND (dbo.CrossoverDesignTable.ApplicationID = @ApplicationID OR @ApplicationID IS NULL)
AND (dbo.CrossoverDesignTable.ShowOnWeb = 'Yes')
AND (dbo.CrossoverDesignTable.StyleID = @StyleID OR @StyleID IS NULL)
RETURN
答案 0 :(得分:0)
假设-1
是您不具备的价值,我建议您使用此值:
ISNULL(dbo.CrossoverDesignTable.ProductID, -1) = ISNULL(@ProductID, -1)
而不是:
dbo.CrossoverDesignTable.ProductID = @ProductID OR @ProductID IS NULL
并以同样的方式进行其他比较。
如果你想保留你的方法,你应该这样做,我想。
( (@ProductID is null) AND (dbo.CrossoverDesignTable.ProductID is null) )
OR (dbo.CrossoverDesignTable.ProductID = @ProductID)
答案 1 :(得分:0)
我在这些情况下所做的是将@ProductId
的默认值更改为-1
@ProductID int = -1,
@CollectionID int = NULL,
@ApplicationID int = NULL,
@StyleID int = NULL
WHERE (@ProductID = -1 OR dbo.CrossoverDesignTable.ProductID = @ProductID)
AND .....