内容简介:翻译自:https://stackoverflow.com/questions/19357990/sql-select-all-if-parameter-is-null-else-return-specific-item
有没有办法编写以下脚本,以便在ProductID变量为null时返回所有产品?并在产品不为空时返回特定产品.
到目前为止我所拥有的:
DECLARE @productID INT = NULL
SELECT
ProductID,
ProductName,
ProductDesc
FROM
product
WHERE
ProductID = @productID
用例陈述:
SELECT ProductID, ProductName,ProductDesc FROM product WHERE ProductID = CASE WHEN @productID IS NULL THEN ProductID ELSE @productID END
或者IIF()函数,如果您使用的是SQL Server 2012:
SELECT ProductID, ProductName,ProductDesc FROM product WHERE ProductID =IIF(@productID IS NULL, ProductID, @productID )
翻译自:https://stackoverflow.com/questions/19357990/sql-select-all-if-parameter-is-null-else-return-specific-item
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。