我想为我缓慢变化的维度DIM_Object提供+1计数器。每当表PMEObject表中的记录更改时,我希望DIM_Object.Versie增加1。
关于代码的所有内容都可以正常运行,这也可以没有任何错误地运行。但是,在我有“ +1 Versie”的地方,我需要它进行实际计数并参考上一行,该行显示了将+1添加到的实际数字。我不知道如何正确引用旧记录。
INSERT INTO DIM_Object
SELECT ObjectId
, Name
, Eff_Date
, End_Date
, Current_Flag
, Versie
FROM ( MERGE DIM_Object CM
USING PMEOBJECT CS
ON ( CM.ObjectId = CS.ObjectId )
WHEN NOT MATCHED
THEN
INSERT VALUES
( CS.ObjectId
, CS.Name
, CONVERT(CHAR(10), GETDATE() - 1, 101)
, '12/31/2199'
, 'y'
, '1'
)
WHEN MATCHED AND CM.Current_Flag = 'y'
AND ( CM.Name <> CS.Name )
THEN
UPDATE SET
CM.Current_Flag = 'n' ,
CM.End_date = CONVERT(CHAR(10), GETDATE() - 2, 101)
OUTPUT
$Action Action_Out
, CS.ObjectId
, CS.Name
, CONVERT(CHAR(10), GETDATE() - 1, 101) Eff_Date
, '12/31/2199' End_Date
, 'y' Current_Flag
, +1 Versie) AS MERGE_OUT
WHERE MERGE_OUT.Action_Out = 'UPDATE';
在我放置了“ +1 Versie”的位置,我希望它根据DIM_Object.Versie的先前编号进行实际累积。现在,它将保持为“ 1”,因为我猜它在当前记录中只会看到NULL。
您有什么想法吗?
亲切的问候,
广告
答案 0 :(得分:0)
此修改可能对您有用吗?我已将子查询添加到您的[插入值...]
Create Table #DIM_Object
(
ObjectId Int,
[Name] NVarChar(25),
End_Date Date,
Eff_Date Date,
Current_Flag Char(1),
Versie Int
)
Insert Into #DIM_Object Values
(1,'Me','2019-01-06','2020-12-31','y',1)
Create Table #PMEObject
(
ObjectId Int,
[Name] NVarChar(25),
End_Date Date,
Eff_Date Date,
Current_Flag Char(1),
Versie Int
)
Insert Into #PMEObject Values
(2,'You','2019-01-30','2019-01-07','n',1),
(1,'Him','2019-01-06','2020-12-31','n',1)
INSERT INTO #DIM_Object
SELECT ObjectId
, Name
, Eff_Date
, End_Date
, Current_Flag
, Versie
FROM (
MERGE #DIM_Object CM
USING #PMEOBJECT CS
ON ( CM.ObjectId = CS.ObjectId )
WHEN NOT MATCHED
THEN
INSERT VALUES
( CS.ObjectId
, CS.Name
, CONVERT(CHAR(10), GETDATE() - 1, 101)
, '12/31/2199'
, 'y'
, (Select Versie From #PMEObject p Where p.ObjectId = CS.ObjectId ) + 1
)
WHEN MATCHED AND CM.Current_Flag = 'y'
AND ( CM.Name <> CS.Name )
THEN
UPDATE SET
CM.Current_Flag = 'n' ,
CM.End_date = CONVERT(CHAR(10), GETDATE() - 2, 101)
OUTPUT
$Action Action_Out
, CS.ObjectId
, CS.Name
, CONVERT(CHAR(10), GETDATE() - 1, 101) Eff_Date
, '12/31/2199' End_Date
, 'y' Current_Flag
, +1 Versie
) AS MERGE_OUT
WHERE MERGE_OUT.Action_Out = 'UPDATE';
结果:
ObjectId Name End_Date Eff_Date Current_Flag Versie
1 Me 2019-01-05 2020-12-31 n 1
2 You 2019-01-06 2199-12-31 y 2
1 Him 2019-01-06 2199-12-31 y 1
答案 1 :(得分:0)
在MERGE中更新的值将以“已删除”的形式返回,您可以在输出中捕获它们。 OUTPUT Clause (Transact-SQL)
INSERT INTO DIM_Object
SELECT ObjectId
, Name
, Eff_Date
, End_Date
, Current_Flag
, Versie
FROM (
MERGE DIM_Object CM
USING PMEOBJECT CS
ON ( CM.ObjectId = CS.ObjectId )
WHEN NOT MATCHED THEN
INSERT VALUES
( CS.ObjectId
, CS.Name
, CONVERT(CHAR(10), GETDATE() - 1, 101)
, '12/31/2199'
, 'y'
, '1'
)
WHEN MATCHED AND CM.Current_Flag = 'y' AND ( CM.Name <> CS.Name ) THEN
UPDATE SET
CM.Current_Flag = 'n' ,
CM.End_date = CONVERT(CHAR(10), GETDATE() - 2, 101)
OUTPUT
$Action Action_Out
, CS.ObjectId
, CS.Name
, CONVERT(CHAR(10), GETDATE() - 1, 101) Eff_Date
, '12/31/2199' End_Date
, 'y' Current_Flag
, deleted.Versie + 1 /* capture the modified row data */
) AS MERGE_OUT
WHERE MERGE_OUT.Action_Out = 'UPDATE';