简化重复的SQL Server查询

时间:2015-05-04 09:37:28

标签: sql-server

我有一个重复的SQL查询,根据subaccounts / years,从subaccounts / years所持有的金额列表填充一个简单的表,但没有一对一的关系。输出用于SSRS报告。

    CREATE TABLE [dbo].[Results](
      [SchemeDescription] [varchar](50) NULL,
      [SubAccountYear] [varchar](15) NULL,
      [RowNumber] [int] NULL,
      [2008] [money] NULL,
      [2009] [money] NULL,
      [2010] [money] NULL,
      [2011] [money] NULL,
      [2012] [money] NULL,
      [2013] [money] NULL,
      [2014] [money] NULL,
      [2015] [money] NULL,
      [2016] [money] NULL,
      [2017] [money] NULL,
      [2018] [money] NULL,
      [2019] [money] NULL,
      [2020] [money] NULL,
      [Processed] [int] NULL
) ON [PRIMARY]
UPDATE res
      SET 
            [2008] = res.[2008] + isnull(x.[2008],0)
            ,[2009] = res.[2009] + isnull(x.[2009],0)
            ,[2010] = res.[2010] + isnull(x.[2010],0)
            ,[2011] = res.[2011] + isnull(x.[2011],0)
            ,[2012] = res.[2012] + isnull(x.[2012],0)
            ,[2013] = res.[2013] + isnull(x.[2013],0)
            ,[2014] = res.[2014] + isnull(x.[2014],0)
            ,[2015] = res.[2015] + isnull(x.[2015],0)
            ,[2016] = res.[2016] + isnull(x.[2016],0)
            ,[2017] = res.[2017] + isnull(x.[2017],0)
            ,[2018] = res.[2018] + isnull(x.[2018],0)
            ,[2019] = res.[2019] + isnull(x.[2019],0)
            ,[2020] = res.[2020] + isnull(x.[2020],0)
            ,processed = **1**
      FROM [Results] res
      INNER JOIN [Other_Income] as x
            ON x.SubAccountYear = Cast(Cast(res.SubAccount AS Int)as Varchar(10)) + '¬' 
+ Cast (YEAR(res.Inception_Date)+ **1** as varchar(4))
                AND x.Processed = 0
          WHERE res.RowNumber = 1

我必须重复上述查询10次以确保所有子帐户年份都分配到适当的子帐户,星号包围的数字需要从1到10进行枚举。

我确信还有另一种方式,但它没有。 任何想法都赞赏。

1 个答案:

答案 0 :(得分:1)

假设我理解正确,您应该使用存储过程包装update语句,该存储过程将处理后的值作为变量。像这样的东西:

CREATE PROCEDURE UpdateResults (
    @Processed int
)
AS
UPDATE res
  SET 
        [2008] = res.[2008] + isnull(x.[2008],0)
        ,[2009] = res.[2009] + isnull(x.[2009],0)
        ,[2010] = res.[2010] + isnull(x.[2010],0)
        ,[2011] = res.[2011] + isnull(x.[2011],0)
        ,[2012] = res.[2012] + isnull(x.[2012],0)
        ,[2013] = res.[2013] + isnull(x.[2013],0)
        ,[2014] = res.[2014] + isnull(x.[2014],0)
        ,[2015] = res.[2015] + isnull(x.[2015],0)
        ,[2016] = res.[2016] + isnull(x.[2016],0)
        ,[2017] = res.[2017] + isnull(x.[2017],0)
        ,[2018] = res.[2018] + isnull(x.[2018],0)
        ,[2019] = res.[2019] + isnull(x.[2019],0)
        ,[2020] = res.[2020] + isnull(x.[2020],0)
        ,processed = @Processed
  FROM [Results] res
  INNER JOIN [Other_Income] as x
        ON x.SubAccountYear = Cast(Cast(res.SubAccount AS Int)as Varchar(10)) + '¬' 
+ Cast (YEAR(res.Inception_Date)+ @Processed as varchar(4))
            AND x.Processed = 0
      WHERE res.RowNumber = 1

然后执行它(可能是在while循环中):

Declare @processed int = 1
While @processed < 11
begin
    exec UpdateResults @processed
    set @processed = @processed + 1
end