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

x
?
Solved

SQL Server trigger to preserve an existing value

Posted on 2014-09-26
6
Medium Priority
?
187 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
[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
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 13

Accepted Solution

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

There are some very powerful Dynamic 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 di…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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