2

ストアドプロシージャがあります。これは非常に単純で、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
4

2 に答える 2

0
                SELECT  *
                FROM    ( SELECT    tbl.* ,
                                    ROW_NUMBER() OVER ( ORDER BY tbl.NDB_No ) rownum
                          FROM      dbo.FoodAbbrev AS tbl
                          WHERE     ISNULL(Status,0) = CASE WHEN @Status > 0 THEN @Status ELSE ISNULL(Status,0) END
                          AND       ISNULL(GroupCd, '') = CASE WHEN @Group = '' THEN ISNULL(GroupCd, '') ELSE @Group END
                        ) seq
                WHERE   seq.rownum BETWEEN @X AND @Y
                ORDER BY seq.rownum
于 2012-09-19T14:07:12.037 に答える
0

これらのすべてのステートメントを、オプションのパラメーターを持つIF次の 1 つのステートメントに置き換えます。SELECT

   SELECT  *
   FROM    ( SELECT    tbl.* ,
             ROW_NUMBER() OVER ( ORDER BY tbl.NDB_No ) rownum
             FROM      dbo.FoodAbbrev AS tbl
             WHERE     1 = 1
             AND (@Group IS NULL OR GroupCd = @Group)
             AND (@Status IS NULL OR Status = @Status)
   ) seq
   WHERE   seq.rownum BETWEEN @X AND @Y
   ORDER BY seq.rownum
于 2012-09-19T13:59:46.247 に答える