跟踪 MERGE 命令及其 OUTPUT 执行的操作

发布于 2024-07-29 06:59:41 字数 1974 浏览 2 评论 0原文

我正在使用以下(长)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我为君王 2024-08-05 06:59:41

我认为在这里使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文