Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Retain previous data and update with new data

Posted on 2013-01-05
4
Medium Priority
?
265 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

618 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