我在SQL Server 2005中有一个存储过程,用于根据从UI传递的参数检索值。
这基本上是对具有许多表的JOIN
的名称的搜索操作。我的问题是,当用户没有提供中间名时,我的程序无法访问该值。
P.FirstName = Coalesce( @FirstName,P.FirstName) AND
P.MiddleName = Coalesce(@MiddleName,P.MiddleName) AND
P.LastName = Coalesce( @LastName,P.LastName) AND
(
P.FirstName = Coalesce(@anyName,P.FirstName) OR
P.MiddleName = Coalesce(@anyName,P.MiddleName) OR
P.LastName = Coalesce(@anyName,P.LastName)
)
我无法删除中间名,因为我在所有上述列中都提供了过滤器选项。
答案 0 :(得分:0)
尝试这样的事情
CASE WHEN @FirstName IS NULL THEN CASE WHEN P.FirstName is NULL THEN 1 ELSE 0 END
ELSE CASE WHEN P.FirstName = Coalesce( @FirstName,P.FirstName) THEN 1 ELSE 0 END
END = 1
答案 1 :(得分:0)
改为:
(P.MiddleName = @MiddleName or P.MiddleName is null or @MiddleName is null)
以下是完整示例:
(P.FirstName = @FirstName or P.FirstName is null or @FirstName is null) AND
(P.MiddleName = @MiddleName or P.MiddleName is null or @MiddleName is null) AND
(P.LastName = @LastName or P.LastName is null or @LastName is null) AND
( @AnyName is null or
P.FirstName = @AnyName or
P.MiddleName = @AnyName or
P.LastName = @AnyName )
答案 2 :(得分:0)
where (MiddleName = Coalesce(@MiddleName ,MiddleName ) or MiddleName is null)
不要忘记在引用MiddleName
的两个位置都这样做