Solved

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

Posted on 2008-06-23
9
706 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

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!

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

623 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