尝试在SSDT中做一些简单的数据比较,但证明有点困难。
在一个数据库中,我有两个我要比较的表。
这些表具有相同的模式,只是不同的表名。我只是想看看这个工具是否能给我一个很好的方法来比较两者中的数据。
即
tblOutput
tblOutput_210314
但是这个选择两个表在一个数据库中相互比较我看不出如何实现。
似乎您只能选择源数据库和目标数据库中存在的表名。由于我的源数据库和目标数据库是相同的,我基本上将我的表与自己进行比较?
任何人都知道通过数据比较实现这一目标的方法吗?
答案 0 :(得分:7)
Data Compare仅支持将2个不同的数据库与匹配的模式进行比较。遗憾的是,使用Data Compare无法实现您的目标。来自help documentation:
要求
比较表格或视图中的数据时,表格或 源数据库中的视图必须与表共享多个属性 或在目标数据库中查看。表格和视图不符合 以下标准未进行比较,未出现在第二个标准上 新数据比较向导的页面:
- 表必须具有匹配的列名,这些列名具有兼容的数据类型。
- 表格,视图和所有者的名称区分大小写。
- 表必须具有相同的主键,唯一索引或唯一约束。
- 视图必须具有相同的唯一聚簇索引。
- 只有在具有相同名称的情况下,才能将表与视图进行比较。
答案 1 :(得分:1)
解决工具限制问题。如果您有两个共同列的表需要进行比较。一种解决方案需要复制数据库并比较视图。
在本例中,我们有一个名为CoffeeTableBooksDatabase的数据库,其中包含两个表table_myBooks和table_aFriendsBooks,其中包含需要比较的公共列作者和标题。
使用备份复制数据库,然后执行还原:
创建一个公共视图:
现在,您只需查看视图比较即可比较两个数据库。这不是推荐的解决方案,但它使用该工具及其当前限制。
答案 2 :(得分:0)
这里的另一个人在SSDT数据比较中存在相同的问题。您可以做的一件事是使用Spreadsheet Compare 2013 program
比较Excel工作簿表中的2个表。
1)将数据从SQL Server表转储到2个单独的excel工作簿中 与“ Sheet 1”相同的工作表名称。
2)运行Spreadsheet Compare程序,输入在步骤1中创建的2个工作簿的文件路径。
3)使用导出结果选项。差异将导出到Excel。
NOTE: Each value change for each field will be a new record in the "Compare Export"
4)在导出结果(Excel)中,您将添加一个包含关键字段的字段 从桌子上。我使用
INDIRECT formula
并替换了 范围值列字母( EXPL:'J7562'从'J'到'B')。 完整的公式如下所示。对于其他需要的关键领域,我不得不做几次。--Key Value from the dataset, [PlanDimensionKey] (E2) ="'" & INDIRECT("'[JET upload LAST change.xlsx]dbo.OperatingPlan (import)'" & "!H" & RIGHT(B2,4)) & "'" --Key Value from the dataset, [Division] (F2) =INDIRECT("'[JET upload LAST change.xlsx]dbo.OperatingPlan (import)'" & "!C" & RIGHT(B2,4)) --Key Value from the dataset, [PeriodKey] (G2) ="'" & INDIRECT("'[JET upload LAST change.xlsx]dbo.OperatingPlan (import)'" & "!I" & RIGHT(B2,4)) & "'" --Returns the FIELD LABEL for each value difference; typed in columnar ordinal position (H2) =CHOOSE(COLUMN(INDIRECT(LEFT(B11,1)&"1")), "ProductCode", "Concept", "DivisionKey", "PlanYear", "PlanVersion", "Year", "Period", "PlanDimensionKey", "PeriodKey", "NetSales", "Volume", "SalesCost", "GrossMargin")
5)然后,您可以使用文本编辑器(NotePad ++)粘贴表数据并编写SQL UPDATE语句
- 在NOTEPAD ++中用逗号替换标签 ...将
\t
替换为,
- 为NOTEPADD ++中的每个记录添加圆括号 ...将
\r\n
替换为)\r\n,(
这不是我做过的最简单的事情,但这是比较数据和编写SQL更改的一种有用的方法。我能够使用此方法相当简单地修改504个值。 Changes:= 7 SKU * 3 Columns * 12 Months * 2 divisions = 504 changes
。 VS SSDT无法比较具有架构差异的表是非常糟糕的。
-电子表格将2013年的导出结果与添加的关键字段进行比较-
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| A | B | C | D | E | F | G | H |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| Range | Old Value | New Value | Description | PlanDimensionKey | Division | PeriodKey | VALUE FIELD |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7562 | '1949.26' | '105627.74' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-01' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7563 | '2023.39' | '133624.49' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-02' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7564 | '2380.1799999999998' | '76481.8' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-03' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7565 | '2880.91' | '76563.55' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-04' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7566 | '2797.08' | '92416.79' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-05' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7567 | '2432.5700000000002' | '91324.19' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-06' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7568 | '3095.74' | '100132.53' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-07' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7569 | '3613.07' | '136506.1' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-08' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7570 | '2146.5700000000002' | '115062.45' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-09' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7571 | '2522.0500000000002' | '121164.3' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-10' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7572 | '2690.11' | '135402.20000000001' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-11' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7573 | '2180.79' | '107210.08' | Entered Value Changed. | '8351-US FOODS' | 1 | '2019-12' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7574 | '1949.26' | '105627.74' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-01' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7575 | '2023.39' | '133624.49' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-02' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7576 | '2380.1799999999998' | '76481.8' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-03' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7577 | '2880.91' | '76563.55' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-04' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7578 | '2797.08' | '92416.79' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-05' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7579 | '2432.5700000000002' | '91324.19' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-06' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7580 | '3095.74' | '100132.53' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-07' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7581 | '3613.07' | '136506.1' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-08' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7582 | '2146.5700000000002' | '115062.45' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-09' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7583 | '2522.0500000000002' | '121164.3' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-10' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7584 | '2690.11' | '135402.20000000001' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-11' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| J7585 | '2180.79' | '107210.08' | Entered Value Changed. | '8351-US FOODS' | 2 | '2019-12' | NetSales |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7610 | '37095.74' | '113500.3' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-01' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7611 | '57068.24' | '150427.1' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-02' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7612 | '42449.3' | '101808.75' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-03' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7613 | '42025.86' | '110008.72' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-04' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7614 | '51727.27' | '125514.74' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-05' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7615 | '45501.57' | '106330.64' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-06' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7616 | '48026.35' | '117350.16' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-07' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7617 | '51493.51' | '148743.64000000001' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-08' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7618 | '40102.230000000003' | '124767.09' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-09' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7619 | '40849.61' | '114226.42' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-10' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7620 | '50533.83' | '155022.29' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-11' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7621 | '42731.83' | '122845.71' | Entered Value Changed. | '8353-US FOODS' | 1 | '2019-12' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7622 | '37095.74' | '113500.3' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-01' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7623 | '57068.24' | '150427.1' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-02' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7624 | '42449.3' | '101808.75' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-03' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7625 | '42025.86' | '110008.72' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-04' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7626 | '51727.27' | '125514.74' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-05' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7627 | '45501.57' | '106330.64' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-06' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7628 | '48026.35' | '117350.16' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-07' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7629 | '51493.51' | '148743.64000000001' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-08' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7630 | '40102.230000000003' | '124767.09' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-09' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7631 | '40849.61' | '114226.42' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-10' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7632 | '50533.83' | '155022.29' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-11' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
| L7633 | '42731.83' | '122845.71' | Entered Value Changed. | '8353-US FOODS' | 2 | '2019-12' | SalesCost |
+-------+----------------------+----------------------+------------------------+------------------+----------+-----------+-------------+
-电子表格比较用NOTEPADD ++写入SQL Update的结果-
USE TestingDb
GO
/*
--Create backup of data
insert into OperatingPlan_bak_20181001_1450 select * from OperatingPlan
--Restore backup of data
delete from OperatingPlan
insert into OperatingPlan select * from OperatingPlan_bak_20181001_1450
*/
-- (#1) Create TempTbl for storing the new values
DECLARE
@updateOperatingPlan_NetSales as table(
-- [ProductCode] [nvarchar](12) NOT NULL
-- ,[Concept] [nvarchar](40) NOT NULL
[DivisionKey] [int] NOT NULL
-- ,[PlanYear] [int] NOT NULL
-- ,[PlanVersion] [int] NOT NULL
-- ,[Year] [int] NOT NULL
-- ,[Period] [nvarchar](6) NOT NULL
,[PlanDimensionKey] [nvarchar](62) NOT NULL
,[PeriodKey] [nvarchar](10) NOT NULL
-- ,[Volume] [numeric](18, 2) NULL
,[NetSales_new] [numeric](18, 2) NULL
-- ,[SalesCost_new] [numeric](18, 2) NULL
-- ,[GrossMargin_new] [numeric](18, 2) NULL
)
DECLARE
@updateOperatingPlan_SalesCost as table(
[DivisionKey] [int] NOT NULL
,[PlanDimensionKey] [nvarchar](62) NOT NULL
,[PeriodKey] [nvarchar](10) NOT NULL
,[SalesCost_new] [numeric](18, 2) NULL
)
DECLARE
@updateOperatingPlan_GrossMargin as table(
[GrossMargin_new] [numeric](18, 2) NULL
,[DivisionKey] [int] NOT NULL
,[PlanDimensionKey] [nvarchar](62) NOT NULL
,[PeriodKey] [nvarchar](10) NOT NULL
)
-- (#2) Insert the new data records into TEMP TABLE
--NetSales_new
INSERT INTO @updateOperatingPlan_NetSales(
[NetSales_new]
,[PlanDimensionKey]
,[DivisionKey]
,[PeriodKey]
)
VALUES --copied from NOTEPAD++ (text) Spreadsheet Compare 2013 (table)
('105627.74', '8351-US FOODS', 1, '2019-01')
,('133624.49', '8351-US FOODS', 1, '2019-02')
,('76481.8', '8351-US FOODS', 1, '2019-03')
,('76563.55', '8351-US FOODS', 1, '2019-04')
,('92416.79', '8351-US FOODS', 1, '2019-05')
,('91324.19', '8351-US FOODS', 1, '2019-06')
,('100132.53', '8351-US FOODS', 1, '2019-07')
,('136506.1', '8351-US FOODS', 1, '2019-08')
,('115062.45', '8351-US FOODS', 1, '2019-09')
,('121164.3', '8351-US FOODS', 1, '2019-10')
,('135402.20000000001', '8351-US FOODS', 1, '2019-11')
,('107210.08', '8351-US FOODS', 1, '2019-12')
,('105627.74', '8351-US FOODS', 2, '2019-01')
,('133624.49', '8351-US FOODS', 2, '2019-02')
,('76481.8', '8351-US FOODS', 2, '2019-03')
,('76563.55', '8351-US FOODS', 2, '2019-04')
,('92416.79', '8351-US FOODS', 2, '2019-05')
,('91324.19', '8351-US FOODS', 2, '2019-06')
,('100132.53', '8351-US FOODS', 2, '2019-07')
,('136506.1', '8351-US FOODS', 2, '2019-08')
,('115062.45', '8351-US FOODS', 2, '2019-09')
,('121164.3', '8351-US FOODS', 2, '2019-10')
,('135402.20000000001', '8351-US FOODS', 2, '2019-11')
,('107210.08', '8351-US FOODS', 2, '2019-12')
,('35923.31', '8352-US FOODS', 1, '2019-01')
,('53496.21', '8352-US FOODS', 1, '2019-02')
,('38034.83', '8352-US FOODS', 1, '2019-03')
,('37044.410000000003', '8352-US FOODS', 1, '2019-04')
,('46855.07', '8352-US FOODS', 1, '2019-05')
,('43004.52', '8352-US FOODS', 1, '2019-06')
,('45897.82', '8352-US FOODS', 1, '2019-07')
,('52093.120000000003', '8352-US FOODS', 1, '2019-08')
,('41391.46', '8352-US FOODS', 1, '2019-09')
,('42194.02', '8352-US FOODS', 1, '2019-10')
,('48761.78', '8352-US FOODS', 1, '2019-11')
,('42905.64', '8352-US FOODS', 1, '2019-12')
--SalesCost_new
INSERT INTO @updateOperatingPlan_SalesCost(
[SalesCost_new]
,[PlanDimensionKey]
,[DivisionKey]
,[PeriodKey]
)
VALUES --copied from NOTEPAD++ (text) Spreadsheet Compare 2013 (table)
('108676.8', '8351-US FOODS', 1, '2019-01')
,('142074.69', '8351-US FOODS', 1, '2019-02')
,('85073.74', '8351-US FOODS', 1, '2019-03')
,('86439.09', '8351-US FOODS', 1, '2019-04')
,('101436.04', '8351-US FOODS', 1, '2019-05')
,('96085.38', '8351-US FOODS', 1, '2019-06')
,('104244.97', '8351-US FOODS', 1, '2019-07')
,('134166.29', '8351-US FOODS', 1, '2019-08')
,('110776.14', '8351-US FOODS', 1, '2019-09')
,('116436.68', '8351-US FOODS', 1, '2019-10')
,('139488.53', '8351-US FOODS', 1, '2019-11')
,('106055.85', '8351-US FOODS', 1, '2019-12')
,('108676.8', '8351-US FOODS', 2, '2019-01')
,('142074.69', '8351-US FOODS', 2, '2019-02')
,('85073.74', '8351-US FOODS', 2, '2019-03')
,('86439.09', '8351-US FOODS', 2, '2019-04')
,('101436.04', '8351-US FOODS', 2, '2019-05')
,('96085.38', '8351-US FOODS', 2, '2019-06')
,('104244.97', '8351-US FOODS', 2, '2019-07')
,('134166.29', '8351-US FOODS', 2, '2019-08')
,('110776.14', '8351-US FOODS', 2, '2019-09')
,('116436.68', '8351-US FOODS', 2, '2019-10')
,('139488.53', '8351-US FOODS', 2, '2019-11')
,('106055.85', '8351-US FOODS', 2, '2019-12')
,('37095.74', '8352-US FOODS', 1, '2019-01')
,('57068.24', '8352-US FOODS', 1, '2019-02')
,('42449.3', '8352-US FOODS', 1, '2019-03')
,('42025.86', '8352-US FOODS', 1, '2019-04')
,('51727.27', '8352-US FOODS', 1, '2019-05')
,('45501.57', '8352-US FOODS', 1, '2019-06')
,('48026.35', '8352-US FOODS', 1, '2019-07')
,('51493.51', '8352-US FOODS', 1, '2019-08')
,('40102.230000000003', '8352-US FOODS', 1, '2019-09')
,('40849.61', '8352-US FOODS', 1, '2019-10')
,('50533.83', '8352-US FOODS', 1, '2019-11')
,('42731.83', '8352-US FOODS', 1, '2019-12')
--GrossMargin_new
INSERT INTO @updateOperatingPlan_GrossMargin(
[GrossMargin_new]
,[PlanDimensionKey]
,[DivisionKey]
,[PeriodKey]
)
VALUES --copied from NOTEPAD++ (text) Spreadsheet Compare 2013 (table)
('-3049.05', '8351-US FOODS', 1, '2019-01')
,('-8450.2000000000007', '8351-US FOODS', 1, '2019-02')
,('-8591.94', '8351-US FOODS', 1, '2019-03')
,('-9875.5400000000009', '8351-US FOODS', 1, '2019-04')
,('-9019.25', '8351-US FOODS', 1, '2019-05')
,('-4761.1899999999996', '8351-US FOODS', 1, '2019-06')
,('-4112.4399999999996', '8351-US FOODS', 1, '2019-07')
,('2339.8000000000002', '8351-US FOODS', 1, '2019-08')
,('4286.3100000000004', '8351-US FOODS', 1, '2019-09')
,('4727.63', '8351-US FOODS', 1, '2019-10')
,('-4086.33', '8351-US FOODS', 1, '2019-11')
,('1154.23', '8351-US FOODS', 1, '2019-12')
,('-3049.05', '8351-US FOODS', 2, '2019-01')
,('-8450.2000000000007', '8351-US FOODS', 2, '2019-02')
,('-8591.94', '8351-US FOODS', 2, '2019-03')
,('-9875.5400000000009', '8351-US FOODS', 2, '2019-04')
,('-9019.25', '8351-US FOODS', 2, '2019-05')
,('-4761.1899999999996', '8351-US FOODS', 2, '2019-06')
,('-4112.4399999999996', '8351-US FOODS', 2, '2019-07')
,('2339.8000000000002', '8351-US FOODS', 2, '2019-08')
,('4286.3100000000004', '8351-US FOODS', 2, '2019-09')
,('4727.63', '8351-US FOODS', 2, '2019-10')
,('-4086.33', '8351-US FOODS', 2, '2019-11')
,('1154.23', '8351-US FOODS', 2, '2019-12')
,('-1172.43', '8352-US FOODS', 1, '2019-01')
,('-3572.03', '8352-US FOODS', 1, '2019-02')
,('-4414.4799999999996', '8352-US FOODS', 1, '2019-03')
,('-4981.45', '8352-US FOODS', 1, '2019-04')
,('-4872.2', '8352-US FOODS', 1, '2019-05')
,('-2497.0500000000002', '8352-US FOODS', 1, '2019-06')
,('-2128.5300000000002', '8352-US FOODS', 1, '2019-07')
,('599.6', '8352-US FOODS', 1, '2019-08')
,('1289.23', '8352-US FOODS', 1, '2019-09')
,('1344.41', '8352-US FOODS', 1, '2019-10')
,('-1772.05', '8352-US FOODS', 1, '2019-11')
,('173.8', '8352-US FOODS', 1, '2019-12')
;WITH cteOperatingPlanUpdates as
(
SELECT
t1.PlanDimensionKey
,t1.DivisionKey
,t1.PeriodKey
,t1.NetSales_new
,t2.SalesCost_new
,t3.GrossMargin_new
FROM
@updateOperatingPlan_NetSales t1
LEFT OUTER JOIN @updateOperatingPlan_SalesCost t2
ON t1.PlanDimensionKey = t2.PlanDimensionKey
AND t1.DivisionKey = t2.DivisionKey
AND t1.PeriodKey = t2.PeriodKey
LEFT OUTER JOIN @updateOperatingPlan_GrossMargin t3
ON t1.PlanDimensionKey = t3.PlanDimensionKey
AND t1.DivisionKey = t3.DivisionKey
AND t1.PeriodKey = t3.PeriodKey
)
--select * from cteOperatingPlanUpdates
--/*
-- (#3) Update TargetTbl values with TempTbl values
UPDATE [dbo].[OperatingPlan]
SET
[NetSales] = source.NetSales_new
,[SalesCost] = source.SalesCost_new
,[GrossMargin] = source.GrossMargin_new
FROM cteOperatingPlanUpdates source
WHERE
OperatingPlan.[PlanDimensionKey] = source.PlanDimensionKey
AND OperatingPlan.[DivisionKey] = source.DivisionKey
AND OperatingPlan.[PeriodKey] = source.PeriodKey
GO
--*/