Solved

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

Posted on 2008-06-23
9
660 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
  • 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
 

Author Comment

by:foxc51
ID: 21846496
Sorry, should be fld_repid.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now