Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create a T-SQL Trigger to update a table

Posted on 2008-06-23
9
Medium Priority
?
713 Views
Last Modified: 2012-08-13
I have a Helpdesk package that runs on SQL 2005. When a new email comes into the Helpdesk package I must manually assign the call to IT managers in branch offices. Can I create a trigger to automatically assign a ticket to an IT manager depending on the email domain of the sender?

There is a table called tbl_issue in which a new row is created each time a new ticket is generated, a field containing the email address of the sender and a field containing a number relating to an  IT Manager the ticket has been assigned to. A table named tbl_ref contains the name of the IT Managers. A field name ID contained in tbl_ref links to fld_aref in tbl_issue.

I'm spending a good 4 hours a day manually assigning calls so, it would be a great if someone can help me automate this task.

0
Comment
Question by:foxc51
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21846262
Can you provide a couple of lines of sample data from your tables so I can visually see how they relate?  From there I can probably write the trigger for you.
0
 

Author Comment

by:foxc51
ID: 21846400
Hi, attached is an Excel sheet with the tables in.
Helpdesk-Tables.xls
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21846448
I don't see the field named fld_aref in tbl_issue
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:foxc51
ID: 21846496
Sorry, should be fld_repid.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21846526
OK...to make sure I understand this right.

The two records in tbl_issue haven't been assigned yet.  Youd like for them to be assigned (based on the domain in fldEmail) to a user in tbl_ref.  Does that sound right?
0
 

Author Comment

by:foxc51
ID: 21846549
Yes, that's what I am hoping.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21846670
OK..give this one a shot.  This will pick the first ID from the ref table and assign it to the ticket if the domains from the email fields match up.  


create trigger tr_tblissue on tbl_issue for insert
as
begin
 
update u
set fldREP_ID = 
(
	select max(id) from tbl_ref r
	where substring(r.fldEmail, charindex('@',r.fldEmail)+1, charindex('.', r.fldEmail, charindex('@',r.fldEmail)) - charindex('@',r.fldEmail)-1) = 
	substring(u.fldEmail, charindex('@',u.fldEmail)+1, charindex('.', u.fldEmail, charindex('@',u.fldEmail)) - charindex('@',u.fldEmail)-1)
 )
from tbl_issue u
join inserted i on u.id = i.id
 
end

Open in new window

0
 

Author Comment

by:foxc51
ID: 21847646
OK. Works! However, this there a way to start reading the tbl_ref from record 100?
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 21847661
sure, I just added some criteria to the where clause of the subquery

create trigger tr_tblissue on tbl_issue for insert
as
begin
 
update u
set fldREP_ID =
(
        select max(id) from tbl_ref r
        where substring(r.fldEmail, charindex('@',r.fldEmail)+1, charindex('.', r.fldEmail, charindex('@',r.fldEmail)) - charindex('@',r.fldEmail)-1) =
        substring(u.fldEmail, charindex('@',u.fldEmail)+1, charindex('.', u.fldEmail, charindex('@',u.fldEmail)) - charindex('@',u.fldEmail)-1) and ID > 100
 )
from tbl_issue u
join inserted i on u.id = i.id
 
end
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question