ストアドプロシージャがあります。これは非常に単純で、selectステートメントは1つだけですが、where句はパラメータに基づいて変化します。
if elseステートメントを使用してselectを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