What is the proper structure for an insert after SQL trigger?

This is my first attempt at an INSERT AFTER trigger. What I need to happen is if the transactions are coming from timesheets(SOURDOC='PATS') I need to replace the employee ID(ORMSTRID) with another value that is held in a user defined field on the employee card(UPR00100). In the GL20000 table the jrnentry field is the primary key. This trigger appears to work but do I need to add anything to it for data validation?

create trigger [dbo].[ORMSTRID] on [dbo].[GL20000] after insert
as
begin


UPDATE dbo.GL20000
      SET ORMSTRID=T2.USERDEF1
      FROM dbo.GL20000 T1
      INNER JOIN dbo.UPR00100 T2 ON T1.ORMSTRID=T2.EMPLOYID
      INNER JOIN inserted ins on T1.JRNENTRY=ins.JRNENTRY
      WHERE T1.SOURCDOC='PATS'

end
LVL 1
rwheeler23Asked:
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.

OMC2000Commented:
You could validate how many records were inserted and inserted values. The first check could be omitted in your case due to inner join in your update statement. The second type of check also seems to be unnecessary in your case because you transparently do it in your update statement, for example:
T1.SOURCDOC='PATS'
rwheeler23Author Commented:
Thanks for the tip. It made me remember that I needed to add the SEQNUMBR field to the JOIN. This way only one record will be inserted at a time.

create trigger [dbo].[ORMSTRID] on [dbo].[GL20000] after insert
as
begin


UPDATE dbo.GL20000
      SET ORMSTRID=T2.USERDEF1
      FROM dbo.GL20000 T1
      INNER JOIN dbo.UPR00100 T2 ON T1.ORMSTRID=T2.EMPLOYID
      INNER JOIN inserted ins on T1.JRNENTRY=ins.JRNENTRY and T1.SEQNUMBR=ins.SEQNUMBR
      WHERE T1.SOURCDOC='PATS'

end
OMC2000Commented:
If you execute a statement like: insert into GL20000 (select ....), where select returns multiple records, inserted holds all these records. And I don’t think added condition “and T1.SEQNUMBR=ins.SEQNUMBR” could change number of records processed by this update statement. Each record in inserted presents in GL20000 and will be affected by this update.

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
Ensure Business Longevity with As-A-Service

Using the as-a-service approach for your business model allows you to grow your revenue stream with new practice areas, without forcing you to part ways with existing clients just because they don’t fit the mold of your new service offerings.

rwheeler23Author Commented:
OK, so let's say jrnentry = 4321 and there are 4 sequence lines. Each line gets inserted one by one.
Seq line 1 gets inserted and I only want ORMSTRID to be updated on this one line. Isn't this what my insert trigger will do? It will only be doing one line at a time?
OMC2000Commented:
If multiple inserts based on statements like
insert into GL20000 (select ....)
are not expected. This extra condition is necessary and correct. Otherwise it won't help.
rwheeler23Author Commented:
Thanks
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.