Solved

Retain previous data and update with new data

Posted on 2013-01-05
4
240 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now