gpt4 book ai didi

sql - SQL 中 WHERE 子句中的 LIKE 和 NULL

转载 作者:行者123 更新时间:2023-12-02 07:42:36 27 4
gpt4 key购买 nike

我有一个存储过程,我计划将其用于搜索并获取所有值。

场景:如果传递的参数为NULL,则应返回表中的所有值,如果传递的参数不为NULL,则应根据LIKE中的条件返回值.

//查询:

ALTER procedure [dbo].[usp_GetAllCustomerDetails]
(
@Keyword nvarchar(20) = null
)
As
Begin

Select CustomerId,CustomerName,CustomerTypeName,CustomerCode,CategoryName,CustomerMobile,CustomerEmail,CustomerAddress,CustomerCity,CustomerState,Pincode
from tblCustomerMaster CM
inner join dbo.tblCustomerTypeMaster CTM on CTM.CustomerTypeId = CM.CustomerType
inner join dbo.tblCategoryMaster CCM on CCM.CategoryId= CM.CustomerCategory
where CustomerName like '%'+@Keyword+'%'

在上面的查询中,当我执行时,它不返回任何值,因为NULLSQL假定为string,所以我应该写什么在 where 子句中获得所需的输出?

最佳答案

您可以在 where 子句中使用这样的条件

where @Keyword is null or CustomerName like '%' + @Keyword + '%' 

关于sql - SQL 中 WHERE 子句中的 LIKE 和 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18683530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com