2
我有一個存儲過程,它非常簡單,只有一個select語句,但where子句根據參數進行更改。sql server 2008存儲過程優化
我使用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
厄蘭Sommarskog的優秀【動態搜索條件在SQL](HTTP://www.sommarskog。 se/dyn-search.html)通常會是一個很好的開始。 –