I have a table called Sales
and another SalesHistory
. SalesHistory
is the replica of Sales
table.
Now the Sales
table can be dropped anytime and recreated again with new columns being added and old columns being renamed to something different. I had written a stored procedure which copies data from the sales
table to saleshistory
table depending upon a condition if it needs insert or update
Now I am bit lost: how do I fix the issue that once sales table is dropped and recreated, how can I amend those changes to the saleshistory table?
Any idea or or same code, I can share my code off stored procedure if need but that is pretty simple
Here is the code
Insert into SalesHistory (Cusip, KeyFeatures1, KeyFeatures2, KeyFeatures3, KeyFeatures4, KeyFeatures5, KeyFeatures6, KeyFeatures7, KeyRisks1, KeyRisks2, KeyRisks3, Comments1, Comments2, Comments3)
select
Cusip, KeyFeatures1, KeyFeatures2, KeyFeatures3, KeyFeatures4,
KeyFeatures5, KeyFeatures6, KeyFeatures7, KeyRisks1, KeyRisks2,
KeyRisks3, Comments1, Comments2, Comments3
from
Sales
where
not exists (SELECT 1 FROM SalesHistory WHERE cusip = Sales.cusip)
UPDATE Hist
SET Cusip = A.Cusip,
KeyFeatures1 = A.KeyFeatures1,
KeyFeatures2 = A.KeyFeatures2,
KeyFeatures3 = A.KeyFeatures3,
KeyFeatures4 = A.KeyFeatures4,
KeyFeatures5 = A.KeyFeatures5,
KeyFeatures6 = A.KeyFeatures6,
KeyFeatures7 = A.KeyFeatures7,
KeyRisks1 = A.KeyRisks1,
KeyRisks2 = A.KeyRisks2,
KeyRisks3 = A.KeyRisks3,
Comments1 = A.Comments1,
Comments2 = A.Comments2,
Comments3 = A.Comments3
FROM
SalesHistory Hist
INNER JOIN
Sales A ON A.cusip = Hist.cusip
I have already explained in my question what I am trying to do