用户可以按名字,姓氏搜索客户,也可以选择进入城市。
只有当用户使用动态SQL输入一个没有w / o的SQL时,是否可以编写与CITY匹配的SQL?
CREATE PROCEDURE [dbo].[SearchCustomer]
@FirstName varchar(30) --REQUIRED
@LastName varchar(30)--REQUIRED
@City varchar(30) --OPTIONAL
AS
SELECT * FROM CUSTOMER C WHERE
C.FirstName = @FirstName AND
C.LastName = @LastName AND
C.City = IsNull(@City, C.City) --This won't Work if CITY is optional in the database
答案 0 :(得分:6)
尝试:
(@City is null OR C.City = @City)
答案 1 :(得分:2)
答案 2 :(得分:0)
CREATE PROCEDURE [dbo].[SearchCustomer]
@FirstName varchar(30) --REQUIRED
@LastName varchar(30)--REQUIRED
@City varchar(30) --OPTIONAL
AS
IF @City is NULL
BEGIN
SELECT * FROM CUSTOMER C WHERE
C.FirstName = @FirstName AND
C.LastName = @LastName
END
ELSE
BEGIN
SELECT * FROM CUSTOMER C WHERE
C.FirstName = @FirstName AND
C.LastName = @LastName AND
C.City = @City
END