存储过程有多个过滤器-所有过滤器都在一个过程中,或者每个过滤器分成独立的过程

时间:2018-07-25 11:25:55

标签: sql sql-server sql-server-2008

我有一个存储过程,在这里我传递带有过滤器类型的参数,并传递带有过滤器值的第二个参数。可以是游戏类型,用户类型等。

我想根据不同的类型过滤数据。如果是游戏类型,则如果按用户类型名称输入用户名,则应按game_name列(无论将其作为参数传递)进行过滤。

从设计的角度来看,我想知道是将多个case语句放入一个存储过程中还是为不同的过滤器类型创建每个存储过程更好,最后我将以相同的核心获得5-6个不同的存储过程sql(选择语句)。

过程示例:

ALTER PROCEDURE [dbo].[Reports_UserStatsDaily] @network       VARCHAR(9) = NULL, 
                                               @playerAddress VARCHAR(42) = NULL, 
                                               @year          INT = NULL, 
                                               @month         INT = NULL,
                                               @from VARCHAR(15) = null,
                                               @to VARCHAR(15) = null
AS 
  BEGIN 
      -- SET NOCOUNT ON added to prevent extra result sets from 
      -- interfering with SELECT statements. 
      SET nocount ON; 

      -- Insert statements for procedure here 
      SELECT [playeraddress], 
             [network], 
             [rounds], 
             [sessions], 
             [handle], 
             [hold], 
             Datefromparts([year], [month], [day]) AS [Date] 
      FROM   [dbo].[userstatsdaily] 
      WHERE  ( @network IS NULL 
                OR ( network = Upper(@network) ) ) 
             AND ( @playerAddress IS NULL 
                    OR ( playeraddress = Upper(@playerAddress) ) ) 
             AND ( @year IS NULL 
                    OR [year] = @year ) 
             AND ( @month IS NULL 
                    OR [month] = @month ) 

             AND ( @from IS NULL 
                    OR ( Datefromparts([year], [month], [day]) BETWEEN 
                             Cast(@from AS DATETIME2) AND Cast(@to AS DATETIME2) 
                       ) 
                 ) 
            AND (len(Handle) > 9 or len(Hold) > 9)
      ORDER  BY [year] ASC, 
                [month] ASC, 
                [day] ASC 
  END 

这里的问题是,我放置了更多的过滤器,放置了更多的可选参数,最后在WHERE子句中追加了。如何在存储过程中实现关注点分离?

0 个答案:

没有答案