?
Solved

Retain previous data and update with new data

Posted on 2013-01-05
4
Medium Priority
?
261 Views
Last Modified: 2013-01-10
Hi Experts,

I have Source.Warehouse.SafetyStock, source.Warehouse.ReorderStock

and database2.Warehouse.SafetyStock, database2.Warehouse.PrevSafetyStock,database2.Warehouse.ReorderStock,database2.Warehouse.PrevReOrderStock.

What i need to happen is that database2.Warehouse.SafetyStock to always be equal to source.Warehouse.SafetyStock, database2.Warehouse.ReorderStock to always be equal to source.Warehouse.ReorderStock.

Our ERP system will update the source database and I want the database2.Warehouse.SafetyStock to be updated with this value and the PrevSafetyStock to hold the former source.Warehouse.SafetyStock...

Hopefully this makes sense
0
Comment
Question by:Mikeyman_01
[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
  • 2
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 38748539
The tags suggest that this is in a trigger; therefore, one option is to use the DELETED and INSERTED tables. In other words, DELETED.SafetyStock is the previous, removed value; INSERTED.SafetyStock, the new. Hence, an UPDATE with a JOIN should suffice.

UPDATE tto
SET tto.SafetyStock = i.SafetyStock
  , tto.PrevSafetyStock = d.PrevSafetyStock
FROM database2.Warehouse tto
JOIN DELETED d ON d.PK = tto.PK /* e.g., Item ID */
JOIN INSERTED i ON i.PK = tto.PK /* should be same as above. */
;

Open in new window


In my experience, the SET operations happen sequentially, but I showed example above as I do not believe you want to depend on the order of assignments. However, in theory, the following also should work.

UPDATE tto
SET tto.PrevSafetyStock = tto.SafetyStock
  , tto.SafetyStock = i.SafetyStock
FROM database2.Warehouse tto
JOIN INSERTED i ON i.PK = tto.PK /* e.g., Item ID */
;

Open in new window


Or have another trigger on the database2.Warehouse table that maintains the PrevSafetyStock, but it would be prudent to reduce the amount of triggers (i.e., complexity) in the process.

I hope that helps!
0
 

Author Comment

by:Mikeyman_01
ID: 38749085
Should this be a 'Instead of Insert' or 'After Update' ??

Not sure about the scripting
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 38749521
This trigger would be on the Source.Warehouse table, so you would want the INSERT to occur successfully. Therefore, this would be an AFTER trigger.

i.e.,
CREATE TRIGGER trg_SrcWhs_HandleDb2Sync
ON Source.Warehouse
AFTER INSERT, UPDATE
AS 
/* trigger body here. */
GO

Open in new window


MSDN: http://msdn.microsoft.com/en-us/library/ms189799.aspx
0
 

Author Closing Comment

by:Mikeyman_01
ID: 38763322
Thank you mwvisa1
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

771 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