我对某些“产生过多代码”或者没有最优雅的编码方式问题提出质疑:
ALTER FUNCTION [dbo].fn_Function
(@Location NVARCHAR(30) ='ALL')
RETURNS @ReportTable TABLE(DateOfProviValidity date,
DateOfFixumValidity date,
UserID NVARCHAR(50)
)
AS
BEGIN
DECLARE @CurrentMonth DATE
SET @CurrentMonth = dbo.fn_getFirstOfMonth(getdate())
IF @Location IS NOT NULL AND @Location <> 'ALL'
INSERT INTO @ReportTable
SELECT *
FROM dbo.fn_getFinalPData(@CurrentMonth)
WHERE 1 AND Location = @Location
ELSE
INSERT INTO @ReportTable
SELECT *
FROM dbo.fn_getFinalPData(@CurrentMonth)
WHERE 1 AND Location IS NOT NULL
RETURN
END
是否有可能摆脱最初的IF @Location
语句并将其更加贴近Where
子句?
我的问题是case语句不起作用,因为我不能做一个Location equals或IS NOT NULL语句。
答案 0 :(得分:1)
这样的事情怎么样?
INSERT INTO @ReportTable
SELECT *
FROM dbo.fn_getFinalPData(@CurrentMonth)
WHERE ISNULL(@Location, 'ALL') <> 'ALL'
OR Location = @Location
答案 1 :(得分:0)
试试这个,
foreach