Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server存储过程为total添加两个声明的值_Sql_Sql Server 2008_Stored Procedures_Sum - Fatal编程技术网

SQL Server存储过程为total添加两个声明的值

SQL Server存储过程为total添加两个声明的值,sql,sql-server-2008,stored-procedures,sum,Sql,Sql Server 2008,Stored Procedures,Sum,到目前为止,我在存储过程中使用了两个声明的变量: SET @QuestionPoints = (SELECT SUM(points) FROM tb_responses WHERE userid = @UserId AND id = @ID) SET @EventPoints =(SELECT SUM(dbo.tb_events.p

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

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相加以获得总分?我可以使用“+”添加它们并将其分配给第三个声明的变量,还是使用一个全局语句

谢谢


James

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

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)

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

DECLARE @totalPoints INT
SET @totalPoints = @QuestionPoints + @EventPoints
如果不需要它们保留相同的值,则只需覆盖其中一个:

SET@QuestionPoints=@QuestionPoints+@EventPoints

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


SET@QuestionPoints+=@EventPoints

您可以在一条语句中完成此操作

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
                )

非常感谢。只是想知道,还可以使用另一个变量(@TotalPoints)并将前两个变量相加?@James是的,这是可能的,正如@Richard已经在他的一个示例(@TotalPoints)中所示。每个回答我问题的人都回答了我的问题,但包括有关处理空值的信息非常有用。我将不得不处理这一点,因为参与者可能会从回答的问题中获得分数,但不会从出席活动中获得分数(反之亦然)。更合适的标题是“两个声明变量”,而不是值。感谢所有反应如此迅速的人。