Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Update Trigger if columns are the same

Author  Topic 

Mayiko
Starting Member

9 Posts

Posted - 2012-05-16 : 14:10:59
I have two different status columns. One of them is a bottle status, and the other is a reference number status. The reference number status groups a set of bottles. When the status of a bottle is updated to completed, I want to check if all the other bottles under the same reference number are also completed, and if they are, change the reference number status to closed. I am having trouble doing this, but here is what I have so far.


CREATE TRIGGER utr_UpdateReferenceStatus on Sample For Update
AS
IF UPDATE(SampleStatus)
BEGIN

SET NOCOUNT ON;
If (Select s.SampleStatus From Sample s Join inserted i on s.SampleID = i.SampleID) = 5
If (SELECT CASE
WHEN NOT EXISTS(SELECT *
FROM Sample
WHERE (Select SampleStatus from Sample where SampleReferenceID = (Select s.SampleReferenceID from Sample s Join inserted i on s.SampleID = i.SampleID)) <> 5) THEN 1
ELSE 0) = 1
Update Sample Set SampleReferenceStatus = 1 where SampleID = (Select s.SampleID From Sample s Join inserted i on s.SampleID = i.SampleID)

END
GO

Thanks for the help.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-05-16 : 15:32:57
I didn't 100% follow the logic you are implementing in the code, so what I am posting below is based on my understanding of the descriptive text you posted.
UPDATE s SET
SampleReferenceStatus = 1 -- 1 = completed SampleReferenceStatus
FROM
[Sample] s
WHERE
s.SampleReferenceID IN (SELECT DISTINCT SampleReferenceID FROM INSERTED)
AND NOT EXISTS
(
SELECT * FROM [Sample] s2
WHERE s2.SampleReferenceID = s.SampleReferenceID
AND ISNULL(s2.SampleStatus,0) <> 5 -- 5 = completed SampleStatus
);
Go to Top of Page

Mayiko
Starting Member

9 Posts

Posted - 2012-05-16 : 16:08:25
Sorry, I can definitely see where the logic in my original post is hard to understand, so let me try again.

I have a table that contains a bunch of information about samples, which I will call Sample. Two of the columns in the table are the BottleStatus column and the RefStatus column. The BottleStatus tracks individual bottles through a process. The RefStatus tracks a group of bottles through a process.

What I want to do is write a trigger that fires when the Sample table is updated, and the BottleStatus column is affected. If the BottleStatus column is given a value of "5", I want to check all the bottles tied to the same reference numbers as the bottle being updated, and if all the bottles have a status of "5" then update the RefStatus Column to "1".

So, let me try to write this in code logic:



Trigger on Update of Sample Table
{
If (BottleStatus of record being updated = 5)
{
If (If BottleStatus of all bottles tied to the same reference number = 5)
{
Update RefStatus to 1
}
}
}

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-16 : 19:56:43
[code]
CREATE TRIGGER utr_UpdateReferenceStatus on Sample For Update
AS

Update s
set s.RefStatus = '1'
From Sample s
Join inserted i
on s.SampleID = i.SampleID
outer apply (select count(1) as cnt
from Sample
where SampleReferenceID = s.SampleReferenceID
and SampleStatus <> '5'
)tmp
where i.SampleStatus = '5'
and coalesce(tmp.cnt,0)=0
END
GO
[/code]

Make sure you've nested trigger set to false in db

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Mayiko
Starting Member

9 Posts

Posted - 2012-05-16 : 20:51:22
quote:
Originally posted by visakh16


CREATE TRIGGER utr_UpdateReferenceStatus on Sample For Update
AS

Update s
set s.RefStatus = '1'
From Sample s
Join inserted i
on s.SampleID = i.SampleID
outer apply (select count(1) as cnt
from Sample
where SampleReferenceID = s.SampleReferenceID
and SampleStatus <> '5'
)tmp
where i.SampleStatus = '5'
and coalesce(tmp.cnt,0)=0
END
GO


Make sure you've nested trigger set to false in db



It looks like this should work. Could you help me with one change though? While waiting for a response I moved my RefStatus into a different table because I decided to track more data points for the reference numbers. So, I need to do everything you did, but update the RefStatus in the Reference table, not the Sample table, but still based on the same logic constraints used above.

Thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-16 : 20:54:21
ok. then let us know on what basis (columns) you link reference table to Sample table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Mayiko
Starting Member

9 Posts

Posted - 2012-05-16 : 21:00:30
quote:
Originally posted by visakh16

ok. then let us know on what basis (columns) you link reference table to Sample table?



Yeah, I guess that would help.

The sample table and reference table are linked on the ReferenceID column.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-16 : 21:10:35
[code]
CREATE TRIGGER utr_UpdateReferenceStatus on Sample For Update
AS

Update r
set r.RefStatus = '1'
From Reference r
Join Sample s
on. s.ReferenceID = r.ReferenceID
Join inserted i
on s.SampleID = i.SampleID
outer apply (select count(1) as cnt
from Sample
where SampleReferenceID = s.SampleReferenceID
and SampleStatus <> '5'
)tmp
where i.SampleStatus = '5'
and coalesce(tmp.cnt,0)=0
END
GO
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Mayiko
Starting Member

9 Posts

Posted - 2012-05-16 : 21:14:23
Thanks a ton. That is very easy and makes a lot of sense, now that I see it.

Thanks again.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-16 : 21:15:25
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -