【问题标题】:SQL Server Stored Procedure add two declared values for totalSQL Server 存储过程为总计添加两个声明值
【发布时间】:2011-03-04 04:25:35
【问题描述】:

到目前为止,我在存储过程中使用了两个声明的变量:

SET @QuestionPoints = (SELECT SUM(points) 
                       FROM   tb_responses 
                       WHERE  userid = @UserId 
                              AND id = @ID) 
SET @EventPoints =(SELECT SUM(dbo.tb_events.points) 
                   FROM   dbo.tb_attendance 
                          INNER JOIN dbo.tb_events 
                            ON dbo.tb_attendance.eventid = dbo.tb_events.dbid 
                   WHERE  dbo.tb_attendance.userid = @UserID 
                          AND dbo.tb_attendance.didattend = 'Y' 
                          AND dbo.tb_events.id = @ID) 

如何将@QuestionPoints 和@EventPoints 相加以获得总分?我可以使用“+”将它们添加并分配给第三个声明的变量还是有一个整体声明?

谢谢,

詹姆斯

【问题讨论】:

  • 一个更合适的标题应该是“两个声明的变量”而不是值。感谢所有这么快回复的人。

标签: sql sql-server-2008 stored-procedures sum


【解决方案1】:

如果您不再需要这两个组件变量,您可以(重新)使用其中一个变量:

SET @QuestionPoints = ...
SET @EventPoints = ...
SET @QuestionPoints = @QuestionPoints + @EventPoints 

添加SUM() 时要小心,因为它们可以为NULL。 20 + null => null。必要时使用 ISNULL,例如

SET @QuestionPoints = isnull(@QuestionPoints, 0) + isnull(@EventPoints, 0)

如果您仍然需要它们,那么您可以声明第三个。

DECLARE @TotalPoints float  --- or numeric or whatever the type should be
SET @TotalPoints = @QuestionPoints + @EventPoints 

您甚至可以跳过单个变量

SET @QuestionPoints = (SELECT SUM(POINTS) FROM tb_Responses WHERE UserID = @UserId AND ID = @ID)
                      +
                      (SELECT SUM(dbo.tb_Events.Points) FROM  dbo.tb_Attendance INNER JOIN   dbo.tb_Events ON dbo.tb_Attendance.EventID = dbo.tb_Events.dbID WHERE dbo.tb_Attendance.UserID = @UserID AND dbo.tb_Attendance.DidAttend = 'Y' AND dbo.tb_Events.ID = @ID)

【讨论】:

  • 谢谢。只是要知道,也可以使用另一个变量(@TotalPoints)并将前两个加在一起?
  • @James 是的,这是可能的,正如@Richard 在他的一个示例中已经展示的那样(@TotalPoints)。
  • 每个回答我的问题但包括处理空值的信息的人都非常有帮助。我将不得不处理这个问题,因为参与者可能会从回答的问题中获得分数,但不会从出席活动中获得分数(反之亦然)。
【解决方案2】:

如果您需要@QuestionPoints 和@EventPoints 来保留它们的当前值,那么是的,您需要第三个变量:

DECLARE @totalPoints INT
SET @totalPoints = @QuestionPoints + @EventPoints

如果您不需要它们保持相同的值,那么您可以覆盖其中一个:

SET @QuestionPoints = @QuestionPoints + @EventPoints

或者,在最新版本的 SQL 中:

SET @QuestionPoints += @EventPoints

【讨论】:

    【解决方案3】:

    你可以在一个语句中做到这一点

    Set @Total =    (
                    Select Sum( Z.points )
                    From    (
                            Select points
                            From tb_responses
                            Where userid = @UserId
                                And Id = @Id
                            Union All
                            Select E.points
                            From dbo.tb_attendance As A
                                Join dbo.tb_events As E
                                    On E.dbid = A.eventid
                            Where A.userid = @UserId
                                And A.didattend = 'Y'
                                And E.Id = @ID
                            ) As Z
                    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      相关资源
      最近更新 更多