我需要将MERGE
一组记录放入一个表中。源中的UpdateType
列确定我是否应该DELETE
在目标中的行不在源中。
所以UpdateType
将等于D
或R
... D=Delta, R=Refresh
如果D
,不DELETE
与目标不匹配
如果R
,DELETE
不与目标不匹配。
我有一个WHILE
迭代单个表的方法,以更好地模拟该过程的工作方式。
我可以在 MERGE 中完成此操作吗?或者我还有什么其他选择?
SQL 小提琴: http ://www.sqlfiddle.com/#!3/9cfe/16
这是我的例子,唯一的问题是......我不能在WHEN NOT MATCHED BY SOURCE
子句中使用源值。
DECLARE @BaseTable TABLE
( RN int
,Store int
,UpdateType char(1)
,ItemNumber int
,Name varchar(50)
)
INSERT INTO @BaseTable
SELECT *
FROM
(
SELECT 1 RN, 1 Store, 'D' UpdateType, 1 ItemNumber, 'Wheel' Name
UNION ALL
SELECT 2, 1, 'D', 1, 'Big Wheel'
UNION ALL
SELECT 3, 1, 'D', 2, 'Light'
UNION ALL
SELECT 4, 1, 'R', 1, 'Wide Wheel'
UNION ALL
SELECT 5, 1, 'D', 1, 'Small Wheel'
UNION ALL
SELECT 5, 1, 'D', 4, 'Trunk'
)B
SELECT bt.* FROM @BaseTable bt
DECLARE @Tab TABLE
( Store int
,UpdateType char(1)
,ItemNumber int
,Name varchar(50)
)
DECLARE @count int = 1
--Loop over each row to mimic how the merge will be called.
WHILE @count <= 5
BEGIN
MERGE INTO @Tab T
USING
(
SELECT bt.RN,
bt.Store,
bt.UpdateType,
bt.ItemNumber,
bt.Name,
tab.Store IsRefresh
FROM @BaseTable bt
LEFT JOIN
( --If ANY previous ITERATION was a 'R' then, all subsequent UpdateType MUST = 'R'
--I'm hoping there is a better way to accomplish this.
SELECT Store
FROM @Tab
WHERE UpdateType = 'R'
GROUP BY Store
HAVING COUNT(Store) > 1
)tab
ON bt.Store = tab.Store
WHERE bt.RN = @count
)S
ON S.Store = T.Store AND S.ItemNumber = T.ItemNumber
WHEN MATCHED THEN
UPDATE
SET T.UpdateType = CASE WHEN S.IsRefresh IS NOT NULL THEN 'R' ELSE S.UpdateType END,
T.Name = S.Name
WHEN NOT MATCHED BY TARGET THEN
INSERT(Store,UpdateType,ItemNumber,Name) VALUES(S.Store,S.UpdateType,S.ItemNumber,S.Name)
--WHEN NOT MATCHED BY SOURCE AND S.UpdateType = 'R' THEN
-- DELETE
;
SET @count = @count + 1
END
SELECT * FROM @Tab
--@Tab Expected Result:
-- 1 'R' 1 'Small Wheel'
-- 1 'R' 4 'Trunk'