Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Trigger on SQL2005 table

Posted on 2011-03-16
7
Medium Priority
?
316 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 61

Expert Comment

by:HainKurt
ID: 35149810
0
 
LVL 61

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 61

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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

916 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