• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Retain previous data and update with new data

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
Mikeyman_01
Asked:
Mikeyman_01
  • 2
  • 2
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
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
 
Mikeyman_01Author Commented:
Should this be a 'Instead of Insert' or 'After Update' ??

Not sure about the scripting
0
 
Kevin CrossChief Technology OfficerCommented:
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
 
Mikeyman_01Author Commented:
Thank you mwvisa1
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now