We help IT Professionals succeed at work.

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

rwheeler23
rwheeler23 asked
on
49 Views
Last Modified: 2019-03-24
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
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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'
rwheeler23President

Author

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
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
rwheeler23President

Author

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?
CERTIFIED EXPERT

Commented:
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.
rwheeler23President

Author

Commented:
Thanks

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.