跟踪 MERGE 命令及其 OUTPUT 执行的操作
我正在使用以下(长)SQL 语句修改类型 2 维度:
INSERT INTO AtlasDataWarehouseReports.District
(
Col01,
Col02,
Col03,
Col04,
Col05,
Col06,
Col07,
Col08,
Col09,
Col10,
StartDateTime,
EndDateTime
)
SELECT
Col01,
Col02,
Col03,
Col04,
Col05,
Col06,
Col07,
Col08,
Col09,
Col10,
CONVERT (DATETIME, CONVERT (Varchar, GetDate(), 101)) AS StartDateTime,
NULL AS EndDateTime
FROM
(
MERGE AtlasDataWarehouseReports.District AS MergeTarget
USING Staging.District as MergeSource
ON MergeTarget.Col01 = MergeSource.Col01
AND MergeTarget.EndDateTime IS NULL
WHEN MATCHED
AND (
MergeTarget.Col02 <> MergeSource.Col02
OR MergeTarget.Col05 <> MergeSource.Col05
)
THEN
UPDATE SET MergeTarget.EndDateTime = CONVERT (DATETIME, CONVERT (Varchar, GetDate(), 101))
WHEN NOT MATCHED
THEN
INSERT
(
Col01,
Col02,
Col03,
Col04,
Col05,
Col06,
Col07,
Col08,
Col09,
Col10,
StartDateTime,
EndDateTime
)
VALUES
(
MergeSource.Col01,
MergeSource.Col02,
MergeSource.Col03,
MergeSource.Col04,
MergeSource.Col05,
MergeSource.Col06,
MergeSource.Col07,
MergeSource.Col08,
MergeSource.Col09,
MergeSource.Col10,
CONVERT (DATETIME, CONVERT (Varchar, GetDate(), 101)),
NULL
)
OUTPUT $Action as MergeAction, MergeSource.*
) AS MergeOutput
WHERE 1=1
AND MergeOutput.MergeAction = 'UPDATE';
我将其作为加载数据仓库的 ETL 的一部分运行。 我想要构建的是一个详细的日志记录系统,可以跟踪日志表/文件等中的所有更改。
在合并期间实际完成的所有工作以及插入中使用的输出都在幕后进行。 我想跟踪参与此查询的所有列和值。
我有什么办法可以捕获这些数据吗?
I am modifying a Type 2 dimension using the following (long) SQL statement:
INSERT INTO AtlasDataWarehouseReports.District
(
Col01,
Col02,
Col03,
Col04,
Col05,
Col06,
Col07,
Col08,
Col09,
Col10,
StartDateTime,
EndDateTime
)
SELECT
Col01,
Col02,
Col03,
Col04,
Col05,
Col06,
Col07,
Col08,
Col09,
Col10,
CONVERT (DATETIME, CONVERT (Varchar, GetDate(), 101)) AS StartDateTime,
NULL AS EndDateTime
FROM
(
MERGE AtlasDataWarehouseReports.District AS MergeTarget
USING Staging.District as MergeSource
ON MergeTarget.Col01 = MergeSource.Col01
AND MergeTarget.EndDateTime IS NULL
WHEN MATCHED
AND (
MergeTarget.Col02 <> MergeSource.Col02
OR MergeTarget.Col05 <> MergeSource.Col05
)
THEN
UPDATE SET MergeTarget.EndDateTime = CONVERT (DATETIME, CONVERT (Varchar, GetDate(), 101))
WHEN NOT MATCHED
THEN
INSERT
(
Col01,
Col02,
Col03,
Col04,
Col05,
Col06,
Col07,
Col08,
Col09,
Col10,
StartDateTime,
EndDateTime
)
VALUES
(
MergeSource.Col01,
MergeSource.Col02,
MergeSource.Col03,
MergeSource.Col04,
MergeSource.Col05,
MergeSource.Col06,
MergeSource.Col07,
MergeSource.Col08,
MergeSource.Col09,
MergeSource.Col10,
CONVERT (DATETIME, CONVERT (Varchar, GetDate(), 101)),
NULL
)
OUTPUT $Action as MergeAction, MergeSource.*
) AS MergeOutput
WHERE 1=1
AND MergeOutput.MergeAction = 'UPDATE';
I am running this as a part of the ETL that loads my data warehouse. What I want to build in is a detailed logging system that can track all the changes in a log table/file etc.
All the work that is actually done during the MERGE and the OUTPUT used in the INSERT, is behind the scenes. I want to track all the columns and values that participated in this query.
Is there any way for me to capture this data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在这里使用 T-SQL MERGE 语句的缺点是您会掩盖实际发生的情况,因此引入日志记录可能需要重复的工作。
只是我的想法,但你似乎在这里重新发明了轮子。
SQL Server Integration Services (SSIS) 为这些精确的处理要求提供了预构建的组件。
例如,您可以使用现有的缓慢变化维度转换组件,或者您可以使用许多较低级别的组件来实现您自己的自定义解决方案,但随后合并日志记录。
I believe the disadvantage of using the T-SQL MERGE statement here is that you will obscure what is actually happening and so introducing logging may require duplicated effort.
Just my thoughts but you seem to be re-inventing the wheel a little here.
SQL Server Integration Services (SSIS) offers pre-built components for these exact processing requirements.
For example, you can use the existing Slowly Changing Dimension Transformation component, or you can use a number of lower level components to implement your own custom solution but subsequently incorporate logging.