Solved

Trigger on SQL2005 table

Posted on 2011-03-16
7
309 Views
Last Modified: 2012-05-11
I have a customer master table that includes a field for territory that I need to know when it changes.
I was looking at a job or a trigger (for realtime results) that I could create that would write a record to a table whenever the territory field was changed.

As a job that I would run nightly I can match the customer master accounts in the "Customer Master" table to a "Master Changes" table and if the territories don't match write a new entry with the new territory into the "Master Changes" table.  While this would work it only updates every 24 hours or whenever it is run.

Is it possible to create a trigger to do the same in order to acheive near realtime results?  If so what might it look like?

Thanks
0
Comment
Question by:jdr0606
  • 3
  • 3
7 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 35149810
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35149859
example trigger (this works for update only, and inserts record whenever there is an update)

deleted table is the old records, inserted is the new updated records... I am checking if there is any change on UserName, and insert those records into audit table...


create trigger trg_customers on customers for update
as
begin
  insert into customers$aud
  select d.CustID, d.UserName, i.UserName, Getdate() from inserted i, deleted d
  where d.CustID=i.CustID and i.UserName<>d.UserName;
end;

Open in new window

0
 
LVL 5

Expert Comment

by:idmedellin
ID: 35150163
--Create a table to register changes
CREATE TABLE auditTerrotori(
      [id] [int] NOT NULL,
      [dterritory] [varchar](50) NOT NULL,
      [iterrotory] [varchar](50) NOT NULL
) ON [PRIMARY]

--Create the trigger in customer table
CREATE TRIGGER Tri_territory
   ON  customer
   AFTER UPDATE
AS
BEGIN
      SET NOCOUNT ON;
      insert into auditTerrotori select d.id, d.territory, i.territory from deleted d join inserted i on d.id = i.id
END
GO
0
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.

 

Author Comment

by:jdr0606
ID: 35152040
Almost, however it creates an audit record for any changes to the customer not just the Sales Territory.

I only want to log a change if the Sales Territory field changes.

I have attached the modified trigger that was suggested.

Thanks
--Create a table to register changes
--drop table dynamicsX.dbo.auditTerrotori
CREATE TABLE dynamicsX.dbo.auditTerrotori(
	  [custnmbr] [char](15) NOT NULL,
      [dterritory] [varchar](50) NOT NULL,
      [iterrotory] [varchar](50) NOT NULL
) ON [PRIMARY]

--Create the trigger in customer table
ALTER TRIGGER [dbo].[Tri_territory]
   ON  [Comp].[dbo].[RM00101]
   AFTER UPDATE
AS 
BEGIN
      SET NOCOUNT ON;
      insert into dynamicsX.dbo.auditTerrotori select d.custnmbr, d.salsterr, i.salsterr from deleted d join inserted i on d.custnmbr = i.custnmbr
END

Open in new window

0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 35152130
look at my sample, Line 6

i.UserName<>d.UserName;

you should do the same thing

      insert into dynamicsX.dbo.auditTerrotori select d.custnmbr, d.salsterr, i.salsterr from deleted d join inserted i on d.custnmbr = i.custnmbr and d.salsterr != i.salsterr

0
 
LVL 5

Expert Comment

by:idmedellin
ID: 35152139
ALTER TRIGGER [dbo].[Tri_territory]
   ON  [Comp].[dbo].[RM00101]
   AFTER UPDATE
AS
BEGIN
      SET NOCOUNT ON;
      insert into dynamicsX.dbo.auditTerrotori select d.custnmbr, d.salsterr, i.salsterr from deleted d join inserted i on d.custnmbr = i.custnmbr and d.Territory <>  i.Territory
END
0
 
LVL 5

Expert Comment

by:idmedellin
ID: 35152155
--Sorry, it is a where clause

ALTER TRIGGER [dbo].[Tri_territory]
   ON  [Comp].[dbo].[RM00101]
   AFTER UPDATE
AS
BEGIN
      SET NOCOUNT ON;
      insert into dynamicsX.dbo.auditTerrotori select d.custnmbr, d.salsterr, i.salsterr
        from deleted d join inserted i
           on d.custnmbr = i.custnmbr
        where d.Territory <>  i.Territory
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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

11 Experts available now in Live!

Get 1:1 Help Now