Solved

SQL Server trigger to preserve an existing value

Posted on 2014-09-26
6
161 Views
Last Modified: 2014-09-29
I have a database field that stores a tracking number for shipments. Occasionally we send the order out in 2 shipments. When we process the 2nd shipment, our shipping software writes the new tracking number into the tracking field removing the tracking number that was already there.
I want to know the syntax to write a trigger for SQL Server that would evaluate the TrackingNumber1 field on update and, if there was already a tracking number in TrackingNumber1, it would capture it and move it to TrackingNumber2. The final result would be that I have both tracking numbers captured and available to me.
Can this be done and how?
Thanks -
0
Comment
Question by:GenesisTech
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Russell Fox
ID: 40346934
"DELETED" and "INSERTED" are what SQL Server calls the original data and the new data, respectively - they're essentially both virtual tables. This will concatenate any number of new tracking numbers as long as [TrackingNumber2] is big enough:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER trgKeepTrackingNumber
   ON ShippingTable
   AFTER UPDATE
AS 
BEGIN
	SET NOCOUNT ON;

	UPDATE shp
		SET [TrackingNumber2] = [TrackingNumber2] + ', ' + old.[TrackingNumber1]
	FROM ShippingTable shp
		JOIN INSERTED new
			ON shp.[ID] = new.[ID]
		JOIN DELETED old
			ON shp.[ID] = new.[ID]
	WHERE old.[TrackingNumber1] <> new.[TrackingNumber1]


END
GO

Open in new window

0
 

Author Comment

by:GenesisTech
ID: 40346943
Russell -

Awesome. Delivered as requested and as needed. THANK YOU!

I will plug this in and give it a try and get back to you.
0
 

Author Comment

by:GenesisTech
ID: 40346945
Russell,

I do have a question that I did not think about. If we mess up the shipment and I go into the record and Delete the Tracking Number will the trigger you wrote still move the tracking number to TrackingNumber2 even though I deleted it?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 13

Accepted Solution

by:
Russell Fox earned 500 total points
ID: 40347006
If you delete just the tracking number, i.e. clear out that field, then yes, it will add the old, deleted number to TrackingNumber2. If you delete the entire shipping record, then no, the whole record will go away. If you don't want it to add the deleted tracking number, then you can exclude that in the trigger:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER trgKeepTrackingNumber
   ON ShippingTable
   AFTER UPDATE
AS 
BEGIN
	SET NOCOUNT ON;

	UPDATE shp
		SET [TrackingNumber2] = [TrackingNumber2] + ', ' + old.[TrackingNumber1]
	FROM ShippingTable shp
		JOIN INSERTED new
			ON shp.[ID] = new.[ID]
		JOIN DELETED old
			ON shp.[ID] = new.[ID]
	WHERE old.[TrackingNumber1] <> new.[TrackingNumber1]
		AND COALESCE(old.[TrackingNumber1], '') <> ''

END
GO

Open in new window

A word to the wise: triggers are often overlooked when someone else has to troubleshoot an issue. Make sure you document the trigger and what it does, preferably in the code you use to update the tracking number. Otherwise, people just see weird behavior and don't know why!
0
 
LVL 13

Expert Comment

by:Russell Fox
ID: 40347014
Also, FWIW, it would be better to create a stored procedure that's called to update the tracking number value, and the proc can handle this rather than a trigger. That's assuming you have any control over the UI code, of course. We use a billing system that I created along with our .Net guy, and he has no SQL code other than to call stored procedures and functions that I've created.
0
 

Author Closing Comment

by:GenesisTech
ID: 40350152
Russell,

Excellent answer - works great. Thanks for your help!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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