我有存储过程来寻找客户,它工作正常。如果Customer_City_Name
表中为空,则我无法检索该行。SP 失败。如何做到这一点我必须得到结果,即使Customer_City_Name
或Customer_Country_Code
IS NULL
EXEC findCustomer null,'%',null,null,
SP代码:
CREATE PROCEDURE findCustomer
@customerNumber NVARCHAR(100),
@customerNamePattern NVARCHAR(35),
@customerCityNamePattern NVARCHAR(35),
@customerCountryCode NVARCHAR(5)
AS
BEGIN
DECLARE @SQL NVARCHAR(4000)
SET @SQL =
'
SELECT
c.Customer_Number,
c.Customer_Name,
c.Postal_Address_Identifier,
c.Customer_Street_Or_Road_Name,
c.Customer_City_Name,
c.Customer_Territory_Code,
c.Customer_Postal_Code,
c.Customer_Country_Code,
c.Telephone_Number,
c.Mobile_Telephone_Number,
c.Fax_Number,
c.Email_Address
FROM Customer c
WHERE c.Customer_Number LIKE ' +
CASE WHEN @customerNumber IS NOT NULL
THEN '''' + @customerNumber + ''''
ELSE 'c.Customer_Number'
END + '
AND c.Customer_Name LIKE ' +
CASE WHEN @customerNamePattern IS NOT NULL
THEN '''' + @customerNamePattern + ''''
ELSE 'c.Customer_Name'
END + '
AND c.Customer_City_Name LIKE ' +
CASE WHEN @customerCityNamePattern IS NOT NULL
THEN '''' +@customerCityNamePattern + ''''
ELSE 'c.Customer_City_Name'
END + '
AND c.Customer_Country_Code LIKE ' +
CASE WHEN @customerCountryCode IS NOT NULL
THEN '''' +@customerCountryCode + ''''
ELSE 'c.Customer_Country_Code'
END
EXEC sp_executesql @SQL