【发布时间】:2016-10-04 02:59:44
【问题描述】:
我有一个名为Sales 的表和另一个SalesHistory。 SalesHistory 是 Sales 表的副本。
现在Sales 表可以随时删除并重新创建,添加新列并将旧列重命名为不同的东西。我编写了一个存储过程,它根据需要插入或更新的条件将数据从sales 表复制到saleshistory 表
现在我有点迷失了:如何解决一旦删除并重新创建 sales 表的问题,我该如何修改 saleshistory 表的这些更改?
任何想法或相同的代码,如果需要,我可以在存储过程中分享我的代码,但这很简单
这里是代码
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
我已经在我的问题中解释了我想要做什么
【问题讨论】:
-
请分享您的代码以及您遇到的问题
-
对于初学者,您需要一个已更改名称的列的列表。然后您可以按照此示例使用
sp_rename:stackoverflow.com/questions/16296622/…。您目前如何生成脚本来更改Sales表? -
我已经添加了我的代码,请检查
-
所以您是说您更改了
Sales表中的列名,现在您的SP 不起作用,因为该列不存在?这里没有复杂的答案——您只需要更改您的存储过程和/或您的SalesHistory表即可。因此,无论您当前使用什么过程来捕获和迁移对Sales的更改,还需要为您的存储过程和/或SalesHistory表包含一个更改脚本。您没有提到您的SalesHistory表是否允许更改。如果没有,您想对添加的新列做什么?将值保留为 NULL? -
您能否澄清一下声明:“现在我有点迷失了:如何解决一旦删除并重新创建了 sales 表,我该如何修改 saleshistory 表的这些更改?”你的意思是数据变化,还是架构列变化
标签: sql-server