如果参数为空,则 SQL 全选,否则返回特定项目

编程入门 行业动态 更新时间:2024-10-27 16:32:17
本文介绍了如果参数为空,则 SQL 全选,否则返回特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法编写以下脚本,以便在 ProductID 变量为 null 时返回所有产品?并在产品不为空时返回特定产品.到目前为止我所拥有的:

Is there a way to write the following script so that it returns all products if the ProductID variable is null ? And return a specific product when the product it is not null. What I have so far:

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):

Or IIF() function if you’re using SQL Server 2012:

SELECT ProductID, ProductName,ProductDesc FROM product WHERE ProductID =IIF(@productID IS NULL, ProductID, @productID )

更多推荐

如果参数为空,则 SQL 全选,否则返回特定项目

本文发布于:2023-10-18 03:04:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   全选   参数   项目   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!