我试图弄清楚这个存储过程。
ALTER procedure [dbo].[GetTotals]
@Service nvarchar(50),
@Country nvarchar(50) = NULL,
@Region nvarchar(50) = NULL
as
SELECT
CASE @Service
WHEN 'Army' THEN Sum(Army)
WHEN 'Navy' THEN Sum(Navy)
When 'Marine_Corps' then Sum(Marine_Corps)
When 'Air_Force' then Sum(Air_Force)
ELSE NULL
END as "ServiceTotal"
FROM
All_Records
WHERE Country = CASE WHEN @Country IS NOT NULL THEN @Country ELSE Country END
AND Region = CASE WHEN @Region IS NOT NULL THEN @Region ELSE Region END
我想要的是如果@Country
不为空,则返回记录where Country = @Country
,否则返回所有国家/地区。我希望Region
具有相同的行为。
我试过Else Is Not Null,而且还有其他!= Null,Else<>空
但没有任何效果。
编辑:
我明白了。我只是没有正确地调用程序。这是工作。我必须调用这个名为GetTotals的程序:
答案 0 :(得分:3)
这通常是这样做的:
WHERE COALESCE(@Country,Country) = Country AND COALESCE(@Region,Region) = Region
答案 1 :(得分:1)
WHERE (Country = @Country OR @Country IS NULL) AND (Region = @Region OR @Region IS NULL)