SQL Server INSERT语句的OUTPUT子句不返回标识值

时间:2016-08-09 14:28:22

标签: sql-server triggers sql-server-2008-r2 output-clause

在SQL Server 2008 R2数据库服务器上,我尝试将数据插入表中,但INSERT语句的OUTPUT子句仅返回表的Identity列的零。下面的INSERT语句是一个较大的脚本的一部分,其中包含许多语句包含在单个事务中。

以下是声明:

INSERT INTO CountryDivisions (
    [CountryID]
    , [ParentDivisionID]
    , [DivisionCodeISO]
    , [DivisionCodeShort]
    , [DivisionType]
)
    OUTPUT
        inserted.[CountryDivisionID] -- Identity column, returning only zeros!
        , inserted.[CountryID]
        , inserted.[ParentDivisionID]
        , inserted.[DivisionCodeISO]
        , inserted.[DivisionCodeShort]
        , inserted.[DivisionType]
        INTO @NewCountryDivisions
    SELECT
        dc.[CountryID]
        , null
        , COALESCE(mcd.DependentDivisionCodeISO, mcd.ParentCountryAlpha2Code + '-' + mcd.DependentCountryAlpha2Code)
        , mcd.DependentCountryAlpha2Code
        , mcd.DependentDivisionType
    FROM
        @MoreCountryData AS mcd
        JOIN Countries AS dc
            ON dc.Alpha2Code = mcd.DependentCountryAlpha2Code
    WHERE
        NOT EXISTS (
            SELECT *
            FROM CountryDivisions AS cd2
            WHERE
                cd2.[CountryID] = dc.[CountryID]
                AND cd2.[DivisionCodeShort] = mcd.DependentCountryAlpha2Code
        )

这是我从@NewCountryDivisions中选择时看到的内容: enter image description here

以下是表格的定义:

CREATE TABLE [dbo].[CountryDivisions](
    [CountryDivisionID] [int] IDENTITY(1,1) NOT NULL, -- identity column which is only returning zeros!
    [CountryID] [int] NOT NULL,
    [ParentDivisionID] [int] NULL,
    [DivisionCodeISO] [nvarchar](10) NULL,
    [DivisionCodeShort] [nvarchar](10) NULL,
    [DivisionType] [nvarchar](150) NULL,
    [LastUpdatedBy] [sysname] NOT NULL DEFAULT (user_name()),
    [LastUpdatedDateTime] [datetime] NOT NULL DEFAULT (getutcdate()),
 CONSTRAINT [PK_CountryDivisions_CountryDivisionID] PRIMARY KEY NONCLUSTERED 
(
    [CountryDivisionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

我正在使用INSTEAD OF个触发器来更新两个LastUpdated...列。我是新手写的触发器,虽然我不希望这些引起问题,但为了以防万一,这里是他们的定义:

CREATE TRIGGER [dbo].[CountryDivisions_InsertAudit]
    ON [dbo].[CountryDivisions]
    INSTEAD OF INSERT
    AS
    BEGIN
        SET NoCount ON

        INSERT INTO [dbo].[CountryDivisions] (
            -- I am assuming that SQL Server will apply the IDENTITY process to the [CountryDivisionID] column, so I am not inserting it myself.
            [CountryID]
            , [ParentDivisionID]
            , [DivisionCodeISO]
            , [DivisionCodeShort]
            , [DivisionType]
            , [LastUpdatedBy]
            , [LastUpdatedDateTime]
        )
            SELECT
                i.CountryID
                , i.ParentDivisionID
                , i.DivisionCodeISO
                , i.DivisionCodeShort
                , i.DivisionType
                , COALESCE(NULLIF(LTRIM(RTRIM(i.[LastUpdatedBy])), ''), USER_NAME())
                , COALESCE(i.[LastUpdatedDateTime], GETUTCDATE())
            FROM
                inserted AS i

    END

CREATE TRIGGER [dbo].[CountryDivisions_UpdatAudit]
    ON [dbo].[CountryDivisions]
    INSTEAD OF UPDATE
    AS
    BEGIN
        SET NoCount ON

        UPDATE [dbo].[CountryDivisions]
            SET
                CountryID = i.CountryID
                , ParentDivisionID = i.ParentDivisionID
                , DivisionCodeISO = i.DivisionCodeISO
                , DivisionCodeShort = i.DivisionCodeShort
                , DivisionType = i.DivisionType
                , LastUpdatedBy = COALESCE(NULLIF(LTRIM(RTRIM(i.[LastUpdatedBy])), ''), USER_NAME())
                , LastUpdatedDateTime = COALESCE(i.[LastUpdatedDateTime], GETUTCDATE())
            FROM
                [dbo].[CountryDivisions] AS cd
                JOIN inserted AS i
                    JOIN deleted AS d
                        ON d.CountryDivisionID = i.CountryDivisionID
                    ON cd.CountryDivisionID = i.CountryDivisionID
            WHERE
                -- only update records that have actually been updated.
                i.CountryID <> d.CountryID
                OR ISNULL(i.ParentDivisionID, -1) <> ISNULL(d.ParentDivisionID, -1)
                OR ISNULL(i.DivisionCodeISO, '') <> ISNULL(d.DivisionCodeISO, '')
                OR ISNULL(i.DivisionCodeShort, '') <> ISNULL(d.DivisionCodeShort, '')
                OR (
                    ISNULL(i.LastUpdatedBy, '') <> ISNULL(d.LastUpdatedBy, '')
                    AND UPDATE(LastUpdatedBy)
                )
                OR (
                    ISNULL(i.LastUpdatedDateTime, '1900-01-01') <> ISNULL(d.LastUpdatedDateTime, '1900-01-01')
                    AND UPDATE(LastUpdatedDateTime)
                )
    END

1 个答案:

答案 0 :(得分:0)

似乎我的触发器 问题!

根据其他人在另一个Stack Overflow线程中发表的评论,基础表包含OUTPUT触发器的INSERT语句中的INSTEAD OF子句将仅为使用{定义的任何列返回零{1}}功能。

为了解决这个问题,我将触发器重写为IDENTITY(seed, increment)触发器。这也让我将两个触发器合并为一个触发器。

AFTER