SQL server after update trigger multiple records specific record

The following trigger is not working as expected I think it's the IF statement;

If  (SELECT RESOURCE_ID FROM INSERTED) IN ('H-1101', 'CH-1101')
OR (SELECT RESOURCE_ID FROM INSERTED) LIKE ('G-R%')


When multiple records are written to the OPERATION table it is setting the values for the following fields;
           O.USER_1 = TD.BT,
            O.USER_2 = TD.MAT,
            O.USER_3 = TD.WO,
            O.USER_4 = TD.PN,
            O.USER_5 = TD.SN,
            O.UDF_LAYOUT_ID = 'OPR-HDR'


This happens on all records until it reaches a record that matches the criteria from the if statement


Here is the code;

USE [SANDBOX]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER TRIGGER [dbo].[AFTER_INSERT_OPERATION_VR3]
ON [dbo].[OPERATION]
AFTER INSERT
AS

BEGIN
SET NOCOUNT ON  
If  (SELECT RESOURCE_ID FROM INSERTED) IN ('H-1101', 'CH-1101')
OR (SELECT RESOURCE_ID FROM INSERTED) LIKE ('G-R%')


            begin
            UPDATE O
            SET
            O.USER_1 = TD.BT,
            O.USER_2 = TD.MAT,
            O.USER_3 = TD.WO,
            O.USER_4 = TD.PN,
            O.USER_5 = TD.SN,
            O.UDF_LAYOUT_ID = 'OPR-HDR'

            FROM            
            INSERTED AS I INNER JOIN
                                                 OPERATION AS O ON
                                                 I.WORKORDER_TYPE = O.WORKORDER_TYPE AND
                                                 I.WORKORDER_BASE_ID = O.WORKORDER_BASE_ID AND
                                                 I.WORKORDER_LOT_ID = O.WORKORDER_LOT_ID AND
                                                 I.WORKORDER_SPLIT_ID = O.WORKORDER_SPLIT_ID AND
                                                 I.WORKORDER_SUB_ID = O.WORKORDER_SUB_ID
                                     
                                                 INNER JOIN
                                                 VE7_TRIGGERDATA_OPERATION01 AS TD ON
                                                 O.WORKORDER_TYPE = TD.TYPE AND
                                                 O.WORKORDER_BASE_ID = TD.BASE_ID AND
                                                 O.WORKORDER_SPLIT_ID = TD.SPLIT_ID AND
                                                 O.WORKORDER_LOT_ID = TD.LOT_ID AND
                                                 O.WORKORDER_SUB_ID = TD.SUB_ID

            WHERE        
            I.WORKORDER_BASE_ID = TD.BASE_ID AND
            I.WORKORDER_LOT_ID = TD.LOT_ID AND
            I.WORKORDER_SPLIT_ID = TD.SPLIT_ID AND
            I.WORKORDER_SUB_ID = TD.SUB_ID AND
            I.SITE_ID = TD.SITE_ID
            END
END
TPBasintekAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
Try using:

If  EXISTS (SELECT RESOURCE_ID FROM INSERTED WHERE RESOURCE_ID IN ('H-1101', 'CH-1101'))
 OR EXISTS (SELECT RESOURCE_ID FROM INSERTED WHERE RESOURCE_ID LIKE ('G-R%'))

Open in new window

1
MlandaTCommented:
Can also even combine that into a single query:
If  EXISTS (
      SELECT RESOURCE_ID FROM INSERTED 
      WHERE RESOURCE_ID IN ('H-1101', 'CH-1101') OR RESOURCE_ID LIKE ('G-R%')
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TPBasintekAuthor Commented:
It's still doing the same thing see the attached data example.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
The condition needs to be in the WHERE clause to limit which rows get affected:

ALTER TRIGGER [dbo].[AFTER_INSERT_OPERATION_VR3]
ON [dbo].[OPERATION]
AFTER INSERT
AS

BEGIN
SET NOCOUNT ON  

            UPDATE O
            SET
            O.USER_1 = TD.BT,
            O.USER_2 = TD.MAT,
            O.USER_3 = TD.WO,
            O.USER_4 = TD.PN,
            O.USER_5 = TD.SN,
            O.UDF_LAYOUT_ID = 'OPR-HDR'

            FROM            
            INSERTED AS I INNER JOIN
                                                 OPERATION AS O ON
                                                 I.WORKORDER_TYPE = O.WORKORDER_TYPE AND
                                                 I.WORKORDER_BASE_ID = O.WORKORDER_BASE_ID AND
                                                 I.WORKORDER_LOT_ID = O.WORKORDER_LOT_ID AND
                                                 I.WORKORDER_SPLIT_ID = O.WORKORDER_SPLIT_ID AND
                                                 I.WORKORDER_SUB_ID = O.WORKORDER_SUB_ID
                                     
                                                 INNER JOIN
                                                 VE7_TRIGGERDATA_OPERATION01 AS TD ON
                                                 O.WORKORDER_TYPE = TD.TYPE AND
                                                 O.WORKORDER_BASE_ID = TD.BASE_ID AND
                                                 O.WORKORDER_SPLIT_ID = TD.SPLIT_ID AND
                                                 O.WORKORDER_LOT_ID = TD.LOT_ID AND
                                                 O.WORKORDER_SUB_ID = TD.SUB_ID

           WHERE        
            (i.RESOURCE_ID IN ('H-1101', 'CH-1101') OR i.RESOURCE_ID LIKE ('G-R%')) AND
           I.WORKORDER_BASE_ID = TD.BASE_ID AND
            I.WORKORDER_LOT_ID = TD.LOT_ID AND
            I.WORKORDER_SPLIT_ID = TD.SPLIT_ID AND
            I.WORKORDER_SUB_ID = TD.SUB_ID AND
            I.SITE_ID = TD.SITE_ID

END
1
TPBasintekAuthor Commented:
I was thinking the same thing and already added it but it's still providing the same results.
0
TPBasintekAuthor Commented:
Needed to include the following link:
I.SEQUENCE_NO = O.SEQUENCE_NO

USE [SANDBOX]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER TRIGGER [dbo].[AFTER_INSERT_OPERATION_VR3]
ON [dbo].[OPERATION]
AFTER INSERT
AS

BEGIN
SET NOCOUNT ON  
If  EXISTS (
      SELECT RESOURCE_ID FROM INSERTED
      WHERE RESOURCE_ID IN ('H-1101', 'CH-1101') OR RESOURCE_ID LIKE ('G-R%')
)

      BEGIN
            UPDATE O
            SET
            O.USER_1 = TD.BT,
            O.USER_2 = TD.MAT,
            O.USER_3 = TD.WO,
            O.USER_4 = TD.PN,
            O.USER_5 = TD.SN,
            O.UDF_LAYOUT_ID = 'OPR-HDR'

            FROM            
            INSERTED AS I INNER JOIN
                                                 OPERATION AS O ON
                                                 I.WORKORDER_TYPE = O.WORKORDER_TYPE AND
                                                 I.WORKORDER_BASE_ID = O.WORKORDER_BASE_ID AND
                                                 I.WORKORDER_LOT_ID = O.WORKORDER_LOT_ID AND
                                                 I.WORKORDER_SPLIT_ID = O.WORKORDER_SPLIT_ID AND
                                                 I.WORKORDER_SUB_ID = O.WORKORDER_SUB_ID AND
                                                 I.SEQUENCE_NO = O.SEQUENCE_NO
                                     
                                                 INNER JOIN
                                                 VE7_TRIGGERDATA_OPERATION01 AS TD ON
                                                 O.WORKORDER_TYPE = TD.TYPE AND
                                                 O.WORKORDER_BASE_ID = TD.BASE_ID AND
                                                 O.WORKORDER_SPLIT_ID = TD.SPLIT_ID AND
                                                 O.WORKORDER_LOT_ID = TD.LOT_ID AND
                                                 O.WORKORDER_SUB_ID = TD.SUB_ID

            WHERE        
             (i.RESOURCE_ID IN ('H-1101', 'CH-1101') OR i.RESOURCE_ID LIKE ('G-R%')) AND
            I.WORKORDER_BASE_ID = TD.BASE_ID AND
            I.WORKORDER_LOT_ID = TD.LOT_ID AND
            I.WORKORDER_SPLIT_ID = TD.SPLIT_ID AND
            I.WORKORDER_SUB_ID = TD.SUB_ID AND
            I.SITE_ID = TD.SITE_ID  
      
      END
END
0
TPBasintekAuthor Commented:
You both provided alternate code for constructing the IF statement that I found to be valuable
0
Scott PletcherSenior DBACommented:
Lol, but an IF statement will never give you correct results, ONLY putting in the WHERE clause will.
1
TPBasintekAuthor Commented:
Actually the original code works after I add the  I.SEQUENCE_NO = O.SEQUENCE_NO to the join and remove the following from the where clause (i.RESOURCE_ID IN ('H-1101', 'CH-1101') OR i.RESOURCE_ID LIKE ('G-R%'))

The other two contributors provided the IF EXISTS .. this I didn't know I could do and know I can apply to this code to filter additional criteria. I also think the criteria should reside in the IF statement  


--- this is the original code with I.SEQUENCE_NO = O.SEQUENCE_NO to the join

USE [SANDBOX]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER TRIGGER [dbo].[AFTER_INSERT_OPERATION_VR3]
ON [dbo].[OPERATION]
AFTER INSERT
AS

BEGIN
SET NOCOUNT ON  
If  (SELECT RESOURCE_ID FROM INSERTED) IN ('H-1101', 'CH-1101')
OR (SELECT RESOURCE_ID FROM INSERTED) LIKE ('G-R%')
      BEGIN
            UPDATE O
            SET
            O.USER_1 = TD.BT,
            O.USER_2 = TD.MAT,
            O.USER_3 = TD.WO,
            O.USER_4 = TD.PN,
            O.USER_5 = TD.SN,
            O.UDF_LAYOUT_ID = 'OPR-HDR'

            FROM            
            INSERTED AS I INNER JOIN
                                                 OPERATION AS O ON
                                                 I.WORKORDER_TYPE = O.WORKORDER_TYPE AND
                                                 I.WORKORDER_BASE_ID = O.WORKORDER_BASE_ID AND
                                                 I.WORKORDER_LOT_ID = O.WORKORDER_LOT_ID AND
                                                 I.WORKORDER_SPLIT_ID = O.WORKORDER_SPLIT_ID AND
                                                 I.WORKORDER_SUB_ID = O.WORKORDER_SUB_ID AND
                                                 I.SEQUENCE_NO = O.SEQUENCE_NO
                                     
                                                 INNER JOIN
                                                 VE7_TRIGGERDATA_OPERATION01 AS TD ON
                                                 O.WORKORDER_TYPE = TD.TYPE AND
                                                 O.WORKORDER_BASE_ID = TD.BASE_ID AND
                                                 O.WORKORDER_SPLIT_ID = TD.SPLIT_ID AND
                                                 O.WORKORDER_LOT_ID = TD.LOT_ID AND
                                                 O.WORKORDER_SUB_ID = TD.SUB_ID

            WHERE        
            I.WORKORDER_BASE_ID = TD.BASE_ID AND
            I.WORKORDER_LOT_ID = TD.LOT_ID AND
            I.WORKORDER_SPLIT_ID = TD.SPLIT_ID AND
            I.WORKORDER_SUB_ID = TD.SUB_ID AND
            I.SITE_ID = TD.SITE_ID  
      
      END
END
0
Scott PletcherSenior DBACommented:
Hmm, interesting, and somewhat odd.  But as long as it matches your requirements.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.