当 运行 批量插入时,温度 table 不会下降

Temp table not dropping when run in batch with inser

因此当 运行 时,以下内容不起作用。我将列 [Target_Id] INT 添加到我的 Create Table 语句中。它现在抛出错误:

Msg 213, Level 16, State 1, Line 1376
Column name or number of supplied values does not match table definition.

这是整批:

--FactSite
IF OBJECT_ID('tempdb..#factSite', 'U') IS NOT NULL
    DROP TABLE #factSite;

CREATE TABLE #factSite
(
    [ID] INT,
    [Target_Id] INT,
    [SYS_CHANGE_OPERATION] NVARCHAR(1),
    DimSite_Id INT,
    DimSubSite_Id INT,
    DimSubParameters_Id INT
);

WITH cteUpivotFactSite AS
(
    SELECT [ID], [SYS_CHANGE_OPERATION], [SiteID], [SubSiteID], 
        [SubParameterVal] AS [SubParameter]
    FROM Staging.AppsFlyerBasic
    CROSS APPLY (
        VALUES ('SubParam1',[SubParam1]),
            ('SubParam2',[SubParam2]),
            ('SubParam3',[SubParam3]),
            ('SubParam4',[SubParam4]),
            ('SubParam5',[SubParam5])
    ) x ([DimensionVal],[SubParameterVal])
)
INSERT INTO #factSite
SELECT src.[ID], NULL, src.[SYS_CHANGE_OPERATION], 
    site.[ID] AS DimSite_Id, subSite.ID AS DimSubSite_Id,
    subParameters.[ID] AS DimParameters_Id
FROM cteUpivotFactSite AS src
INNER JOIN AppsFlyer.DimSite AS site
ON (src.[SiteID] = site.[Name] OR (src.[SiteID] IS NULL AND site.[Name] IS NULL))
INNER JOIN AppsFlyer.DimSubSite AS subSite
ON (src.[SubSiteID] = subSite.[Name] OR (src.[SubSiteID] IS NULL AND subSite.[Name] IS NULL))
INNER JOIN AppsFlyer.DimSubParameters AS subParameters
ON (src.[SubParameter] = subParameters.[Name] OR (src.[SubParameter] IS NULL AND subParameters.[Name] IS NULL))

但是,如果我将批次分成两部分,它就会起作用。

第 1 批:

--FactSite
    IF OBJECT_ID('tempdb..#factSite', 'U') IS NOT NULL
        DROP TABLE #factSite;
    CREATE TABLE #factSite
    (
        [ID] INT,
        [Target_Id] INT,
        [SYS_CHANGE_OPERATION] NVARCHAR(1),
        DimSite_Id INT,
        DimSubSite_Id INT,
        DimSubParameters_Id INT
    );

第 2 批:

WITH cteUpivotFactSite AS
    (
        SELECT [ID], [SYS_CHANGE_OPERATION], [SiteID], [SubSiteID], 
            [SubParameterVal] AS [SubParameter]
        FROM Staging.AppsFlyerBasic
        CROSS APPLY (
            VALUES ('SubParam1',[SubParam1]),
                ('SubParam2',[SubParam2]),
                ('SubParam3',[SubParam3]),
                ('SubParam4',[SubParam4]),
                ('SubParam5',[SubParam5])
        ) x ([DimensionVal],[SubParameterVal])
    )
    INSERT INTO #factSite
    SELECT src.[ID], NULL, src.[SYS_CHANGE_OPERATION], 
        site.[ID] AS DimSite_Id, subSite.ID AS DimSubSite_Id,
        subParameters.[ID] AS DimParameters_Id
    FROM cteUpivotFactSite AS src
    INNER JOIN AppsFlyer.DimSite AS site
    ON (src.[SiteID] = site.[Name] OR (src.[SiteID] IS NULL AND site.[Name] IS NULL))
    INNER JOIN AppsFlyer.DimSubSite AS subSite
    ON (src.[SubSiteID] = subSite.[Name] OR (src.[SubSiteID] IS NULL AND subSite.[Name] IS NULL))
    INNER JOIN AppsFlyer.DimSubParameters AS subParameters
    ON (src.[SubParameter] = subParameters.[Name] OR (src.[SubParameter] IS NULL AND subParameters.[Name] IS NULL))

有谁知道为什么单个批次无法删除和创建临时文件 table,但两个单独的批次有效?

这是因为当临时 table DDL 在同一批次中时,您添加的列 Target_Id 还不是 table 定义的一部分,因此此元数据没有感觉到。在前一批中创建 table 时,会在执行第二批之前检测到您添加的列。要使用尚未但将被添加到同一批次对象中的列,您必须使用动态 SQL 在按如下方式完成修改后访问 table。

--FactSite
    IF OBJECT_ID('tempdb..#factSite', 'U') IS NOT NULL
        DROP TABLE #factSite;
    CREATE TABLE #factSite
    (
        [ID] INT,
        [Target_Id] INT,
        [SYS_CHANGE_OPERATION] NVARCHAR(1),
        DimSite_Id INT,
        DimSubSite_Id INT,
        DimSubParameters_Id INT
    );

declare @query nvarchar(2000)

set @query = N'
WITH cteUpivotFactSite AS
(
    SELECT [ID], [SYS_CHANGE_OPERATION], [SiteID], [SubSiteID], 
        [SubParameterVal] AS [SubParameter]
    FROM Staging.AppsFlyerBasic
    CROSS APPLY (
        VALUES (''SubParam1'',[SubParam1]),
            (''SubParam2'',[SubParam2]),
            (''SubParam3'',[SubParam3]),
            (''SubParam4'',[SubParam4]),
            (''SubParam5'',[SubParam5])
    ) x ([DimensionVal],[SubParameterVal])
)
INSERT INTO #factSite
SELECT src.[ID], NULL, src.[SYS_CHANGE_OPERATION], 
    site.[ID] AS DimSite_Id, subSite.ID AS DimSubSite_Id,
    subParameters.[ID] AS DimParameters_Id
FROM cteUpivotFactSite AS src
INNER JOIN AppsFlyer.DimSite AS site
ON (src.[SiteID] = site.[Name] OR (src.[SiteID] IS NULL AND site.[Name] IS NULL))
INNER JOIN AppsFlyer.DimSubSite AS subSite
ON (src.[SubSiteID] = subSite.[Name] OR (src.[SubSiteID] IS NULL AND subSite.[Name] IS NULL))
INNER JOIN AppsFlyer.DimSubParameters AS subParameters
ON (src.[SubParameter] = subParameters.[Name] OR (src.[SubParameter] IS NULL AND subParameters.[Name] IS NULL)) '

exec sp_executesql @query