Solved

Retain previous data and update with new data

Posted on 2013-01-05
4
245 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
  • 2
  • 2
4 Comments
 
LVL 59

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 59

Accepted Solution

by:
Kevin Cross earned 500 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

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.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

773 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