sql-server – 如果参数为null,则SQL select all all返回特定项

栏目: 数据库 · 发布时间: 6年前

内容简介:翻译自: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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Open Data Structures

Open Data Structures

Pat Morin / AU Press / 2013-6 / USD 29.66

Offered as an introduction to the field of data structures and algorithms, Open Data Structures covers the implementation and analysis of data structures for sequences (lists), queues, priority queues......一起来看看 《Open Data Structures》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具