我有一个存储过程,它非常简单,只有一个 select 语句,但是 where 子句会根据参数而变化。
我正在使用 if else 语句并写出 4 次选择,有没有更好的方法来简化这个存储过程?
IF @Status > 0
BEGIN
IF @Group = ''
BEGIN
SELECT *
FROM ( SELECT tbl.* ,
ROW_NUMBER() OVER ( ORDER BY tbl.NDB_No ) rownum
FROM dbo.FoodAbbrev AS tbl
WHERE Status = @Status
) seq
WHERE seq.rownum BETWEEN @X AND @Y
ORDER BY seq.rownum
END
ELSE
BEGIN
SELECT *
FROM ( SELECT tbl.* ,
ROW_NUMBER() OVER ( ORDER BY tbl.NDB_No ) rownum
FROM dbo.FoodAbbrev AS tbl
WHERE Status = @Status
AND GroupCd = @Group
) seq
WHERE seq.rownum BETWEEN @X AND @Y
ORDER BY seq.rownum
END
END
ELSE
BEGIN
IF @Group = ''
BEGIN
SELECT *
FROM ( SELECT tbl.* ,
ROW_NUMBER() OVER ( ORDER BY tbl.NDB_No ) rownum
FROM dbo.FoodAbbrev AS tbl
) seq
WHERE seq.rownum BETWEEN @X AND @Y
ORDER BY seq.rownum
END
ELSE
BEGIN
SELECT *
FROM ( SELECT tbl.* ,
ROW_NUMBER() OVER ( ORDER BY tbl.NDB_No ) rownum
FROM dbo.FoodAbbrev AS tbl
WHERE GroupCd = @Group
) seq
WHERE seq.rownum BETWEEN @X AND @Y
ORDER BY seq.rownum
END
END
END