Solved

Trigger on SQL2005 table

Posted on 2011-03-16
7
306 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

15 Experts available now in Live!

Get 1:1 Help Now