Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trigger on SQL2005 table

Posted on 2011-03-16
7
Medium Priority
?
315 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
[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
  • 3
  • 3
7 Comments
 
LVL 59

Expert Comment

by:HainKurt
ID: 35149810
0
 
LVL 59

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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 59

Accepted Solution

by:
HainKurt earned 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

730 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