Solved

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

Posted on 2008-06-23
9
684 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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…

832 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