SQL Server 存储过程添加两个声明的值总计

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5189643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:33:01  来源:igfitidea点击:

SQL Server Stored Procedure add two declared values for total

sqlsql-server-2008stored-proceduressum

提问by James

Here is what I have so far using two declared variables in the stored procedure:

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

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) 

How can I add @QuestionPoints and @EventPoints together to get the total points? Can I just add them using "+" and assigned to a third declared variable or have one overall statement?

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

Thanks,

谢谢,

James

詹姆士

回答by RichardTheKiwi

If you don't need the two component variables anymore, you can (re)use one of the variables:

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

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

Be careful though when adding SUM()'s, because they can be NULL. 20 + null => null. Use ISNULL if necessary, e.g.

但是在添加SUM()'s时要小心,因为它们可以是 NULL。 20 + null => null. 如有必要,请使用 ISNULL,例如

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

If you still need them, then you can declare a third one.

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

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

You could even skip the individual variables

您甚至可以跳过单个变量

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)

回答by Matthew Burr

If you need @QuestionPoints and @EventPoints to retain their current values, then yes, you need a third variable:

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

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

If you don't need them to retain their same value, then you can just overwrite one of them:

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

SET @QuestionPoints = @QuestionPoints + @EventPoints

SET @QuestionPoints = @QuestionPoints + @EventPoints

Or, in recent versions of SQL:

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

SET @QuestionPoints += @EventPoints

SET @QuestionPoints += @EventPoints

回答by Thomas

You could do this in a single statement

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

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
                )