SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update a date field with a Trigger when a transaction is posted to another table


Update a date field with a Trigger when a transaction is posted to another table

Author
Message
jeremy 64107
jeremy 64107
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 17
Hello,

I’m wondering if you can please help me.
I have two tables. Customers.UniqueID and Transheaders.AccountID
In Customers I have a field called ZLastSale.
I want to create a Trigger to update the ZLastSale field when a transaction is posted to the Transheaders table which inserts to the TradingDate field so I can capture the last sale date on a Customer.

This is my current example however it is not updating the ZLastdate field.

CREATE TRIGGER trg_LastSaleDate ON dbo.Transheaders
AFTER UPDATE
AS
IF UPDATE(TradingDate)
BEGIN
UPDATE c
SET ZLastSales = i.TradingDate
FROM dbo.Customers AS c
JOIN inserted AS i
ON i.UniqueID = c.UniqueID
JOIN deleted AS d
ON i.AccountID = d.AccountID
AND ( i.TradingDate <> d.TradingDate
OR d.TradingDate IS NULL
) ;
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20888 Visits: 18825
You might be slightly over complicating a simple thing, look through this example
Cool

USE tempdb;
GO
/*
Sample Schema, Client and Transactions
*/
/* Drop the tables if they exist in the database */
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = N'TBL_HEADER'
AND TABLE_SCHEMA = N'dbo')
DROP TABLE dbo.TBL_HEADER;

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = N'TBL_CLIENT'
AND TABLE_SCHEMA = N'dbo')
DROP TABLE dbo.TBL_CLIENT;

/* Create simplified transaction table */
CREATE TABLE dbo.TBL_HEADER
(
HEADER_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,CLIENT_ID INT NOT NULL
,HEADER_DATE DATETIME2(0) NOT NULL DEFAULT(SYSDATETIME())
,HEADER_TRANSACTIONDATE DATETIME2(0) NOT NULL
);

/* Create simplified client table */
CREATE TABLE dbo.TBL_CLIENT
(
CLIENT_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,CLIENT_NAME NVARCHAR(128) NOT NULL
,CLIENT_TRANSACTIONDATE DATETIME2(0) NOT NULL
);
GO

/*
Now for the fun stuff, this is a trigger that updates the
client table every time a new transaction is inserted or
updated in the transaction table. It ignores deletes as
the DELETE keyword is missing in the scope definition which
is the line directly after the target object name
(ON dbo.TBL_HEADER)

The trigger uses the two pseudo tables, [inserted] and [deleted].

For inserts, the inserted values are found in the [inserted]
table but the [deleted] is empty.

For updates, the [inserted] holds the new values and the
[deleted] has the old values. If the value is unchanged,
the trigger ignores the record.
*/

CREATE TRIGGER dbo.TRG_HEADER_UPDATE_CLIENT_TRANSACTIONDATE
/* Schema qualified target object name */
ON dbo.TBL_HEADER
/* DML scope, which data manipulation events will fire the
trigger
*/
AFTER INSERT, UPDATE
AS
/*
Trigger code
*/
UPDATE CL
SET CL.CLIENT_TRANSACTIONDATE = INS.HEADER_TRANSACTIONDATE
FROM inserted INS
INNER JOIN dbo.TBL_CLIENT CL
ON INS.CLIENT_ID = CL.CLIENT_ID
LEFT OUTER JOIN deleted DE
ON CL.CLIENT_ID = DE.CLIENT_ID
/* For inserts there are no records in the [deleted] pseudo table. Therefore it
is referenced by a left outer join, which returns NULL for inserts. The
ISNULL then substitudes the NULL with the current value in the client table to
determine whether to update the record or not.
*/
WHERE INS.HEADER_TRANSACTIONDATE <> ISNULL(DE.HEADER_TRANSACTIONDATE,CL.CLIENT_TRANSACTIONDATE)
;
GO

/* Sample data for clients */
INSERT INTO dbo.TBL_CLIENT (CLIENT_NAME,CLIENT_TRANSACTIONDATE)
VALUES
(N'Alpha' ,'2000-01-01')
,(N'Beta' ,'2000-02-01')
,(N'Gamma' ,'2000-03-01')
,(N'Delta' ,'2000-04-01')
,(N'Epsilon','2000-05-01')
,(N'Zeta' ,'2000-06-01');

/* verify the clients */
SELECT * FROM dbo.TBL_CLIENT;

/* insert a single transactional record */
INSERT INTO dbo.TBL_HEADER (CLIENT_ID,HEADER_TRANSACTIONDATE)
SELECT
CL.CLIENT_ID
,'2001-01-01'
FROM dbo.TBL_CLIENT CL
WHERE CL.CLIENT_NAME = N'Alpha';

/* Results */
SELECT * FROM dbo.TBL_CLIENT;

/* insert multiple transactional records */
INSERT INTO dbo.TBL_HEADER (CLIENT_ID,HEADER_TRANSACTIONDATE)
SELECT
CL.CLIENT_ID
,'2002-01-02'
FROM dbo.TBL_CLIENT CL
WHERE CL.CLIENT_NAME <> N'Alpha';

/* Results */
SELECT * FROM dbo.TBL_CLIENT;
SELECT * FROM dbo.TBL_HEADER;

/* update single transactional record */
UPDATE H
SET H.HEADER_TRANSACTIONDATE = '2003-01-02'
FROM dbo.TBL_CLIENT CL
INNER JOIN dbo.TBL_HEADER H
ON CL.CLIENT_ID = H.CLIENT_ID
WHERE CL.CLIENT_NAME = N'Alpha';

/* Results */
SELECT * FROM dbo.TBL_CLIENT;
SELECT * FROM dbo.TBL_HEADER;

/* update multiple transactional records */
UPDATE H
SET H.HEADER_TRANSACTIONDATE = '2004-03-02'
FROM dbo.TBL_CLIENT CL
INNER JOIN dbo.TBL_HEADER H
ON CL.CLIENT_ID = H.CLIENT_ID
WHERE CL.CLIENT_NAME <> N'Alpha';

/* Results */
SELECT * FROM dbo.TBL_CLIENT;
SELECT * FROM dbo.TBL_HEADER;



Update: Added comments.
jeremy 64107
jeremy 64107
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 17
Wow, that looks complicated!
I didn't write my script I had it provided to me.
I am new to Triggers, so that script really means nothing to me.
Are you able to assist me further? Thank you so much.
David Burrows
David Burrows
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11203 Visits: 9880
The trigger looks OK apart from

ZLastSale, ZLastdate and ZLastSales (which is the correct name)
It only processes updates to Transheaders
What about inserts to Transheaders?


Far away is close at hand in the images of elsewhere.

Anon.


jeremy 64107
jeremy 64107
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 17
The correct name is ZLastDate.

If a new transaction for a particular Customer is entered then the Transheaders table should fire the Trigger and update only that Customers record. (As in if a new invoice is posted this will update the transheaders table)

Thanks.
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20888 Visits: 18825
jeremy 64107 (5/11/2014)
Wow, that looks complicated!
I didn't write my script I had it provided to me.
I am new to Triggers, so that script really means nothing to me.
Are you able to assist me further? Thank you so much.


It looks more complex than it really is ;-)
I added some detailed comments in the code, let us know if you need further assistance.
Cool
David Burrows
David Burrows
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11203 Visits: 9880
In that case use Eirikur's trigger although you could simplify it to

CREATE TRIGGER trg_LastSaleDate ON dbo.Transheaders
AFTER INSERT, UPDATE
AS
IF UPDATE(TradingDate)
BEGIN
UPDATE c
SET c.ZLastDate = i.TradingDate
FROM inserted i
JOIN dbo.Customers c ON c.UniqueID = i.UniqueID
END




Far away is close at hand in the images of elsewhere.

Anon.


jeremy 64107
jeremy 64107
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 17
Thank You.
I ran the script and it worked fine.
When I test the transaction in my system I am getting this error:

Microsoft ODBC SQL Driver SQL Server Conversion failed when converting the varchar value '' to data type int.

Is the data field incorrect?
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20888 Visits: 18825
David Burrows (5/11/2014)
In that case use Eirikur's trigger although you could simplify it to

CREATE TRIGGER trg_LastSaleDate ON dbo.Transheaders
AFTER INSERT, UPDATE
AS
IF UPDATE(TradingDate)
BEGIN
UPDATE c
SET c.ZLastDate = i.TradingDate
FROM inserted i
JOIN dbo.Customers c ON c.UniqueID = i.UniqueID
END




Slight difference in the logic, this code will do an update every time, even if the existing value is equal to the update value.
Cool
David Burrows
David Burrows
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11203 Visits: 9880
Please post DDL for the two tables and the SQL you used to test with.


Far away is close at hand in the images of elsewhere.

Anon.


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search