2

I have the following table:

SystemList

System  Value1  Value2  Value3  Value4  Total

System1   10               30     40
System2           20
System3   10               30
System4           30              40

I have the following in a trigger that works fine but I want to iterate through each of the Systems and update the Total of each row without having to do this in actuality 92 times. Is there a way to do a lookup of the systems and total the values in that row whenever there is an insert or update?

Use Database

UPDATE dbo.SystemList
SET Total = (SELECT (COALESCE(Value1, 0) + COALESCE(Value2, 0) + COALESCE(Value3, 0) + COALESCE(Value4, 0)) AS Total
               FROM dbo.SystemList where System = 'System1') Where System = 'System1'  

UPDATE dbo.SystemList
SET Total = (SELECT (COALESCE(Value1, 0) + COALESCE(Value2, 0) + COALESCE(Value3, 0) + COALESCE(Value4, 0)) AS Total
           FROM dbo.SystemList where System = 'System2') Where System = 'System2'

UPDATE dbo.SystemList
SET Total = (SELECT (COALESCE(Value1, 0) + COALESCE(Value2, 0) + COALESCE(Value3, 0) + COALESCE(Value4, 0)) AS Total
           FROM dbo.SystemList where System = 'System3') Where System = 'System3' 

UPDATE dbo.SystemList
SET Total = (SELECT (COALESCE(Value1, 0) + COALESCE(Value2, 0) + COALESCE(Value3, 0) + COALESCE(Value4, 0)) AS Total
           FROM dbo.SystemList where System = 'System4') Where System = 'System4'

My actual trigger is:

USE [Database]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[Total]
   ON  [dbo].[SystemList]
   AFTER  INSERT,UPDATE
AS 

Begin

UPDATE dbo.SystemList
SET Total = (SELECT (COALESCE(Value1, 0) + COALESCE(Value2, 0) + COALESCE(Value3, 0) + COALESCE(Value4, 0)) AS Total
               FROM dbo.SystemList where System = 'System1') Where System = 'System1'  

UPDATE dbo.SystemList
SET Total = (SELECT (COALESCE(Value1, 0) + COALESCE(Value2, 0) + COALESCE(Value3, 0) + COALESCE(Value4, 0)) AS Total
           FROM dbo.SystemList where System = 'System2') Where System = 'System2'

UPDATE dbo.SystemList
SET Total = (SELECT (COALESCE(Value1, 0) + COALESCE(Value2, 0) + COALESCE(Value3, 0) + COALESCE(Value4, 0)) AS Total
           FROM dbo.SystemList where System = 'System3') Where System = 'System3' 

UPDATE dbo.SystemList
SET Total = (SELECT (COALESCE(Value1, 0) + COALESCE(Value2, 0) + COALESCE(Value3, 0) + COALESCE(Value4, 0)) AS Total
           FROM dbo.SystemList where System = 'System4') Where System = 'System4'   

End
0

1 Answer 1

3

You don't have to specify each value in a separate query like you are doing. This would be simpler.

UPDATE dbo.SystemList SET Total = COALESCE(Value1, 0) + COALESCE(Value2, 0) + COALESCE(Value3, 0) + COALESCE(Value4, 0)) 

Or you can do this even easier. Use a computed column in your table instead and you don't even need a trigger.

Alter TABLE SystemList
    Add Total as isnull(Value1, 0) + isnull(Value2, 0) + isnull(Value3, 0) + isnull(Value4, 0)

https://msdn.microsoft.com/en-us/library/ms188300.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

The first method worked, the second gave me very strange values. It seems to work if there is only one value in one column but not if there is a value in multiple columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.