Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Stored Procedure add two declared values for total

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?

Thanks,

James

like image 571
James Avatar asked Mar 04 '11 04:03

James


3 Answers

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.

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)
like image 128
RichardTheKiwi Avatar answered Nov 14 '22 22:11

RichardTheKiwi


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

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

Or, in recent versions of SQL:

SET @QuestionPoints += @EventPoints

like image 21
Matthew Burr Avatar answered Nov 14 '22 23:11

Matthew Burr


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
                )
like image 20
Thomas Avatar answered Nov 14 '22 23:11

Thomas