2

SQL Server 2008 R2 データベース サーバーで、テーブルにデータを挿入しようとしましたが、INSERT ステートメントの OUTPUT 句は、テーブルの ID 列に対してゼロのみを返します。以下の INSERT ステートメントは、多くのステートメントが 1 つのトランザクションにラップされた、より大きなスクリプトの一部です。

ここに声明があります:

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ここに画像の説明を入力

テーブルの定義は次のとおりです。

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トリガーを使用して 2 つの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
4

1 に答える 1

0

私の引き金問題だったようです!

別の Stack Overflow スレッドで誰かが作成したコメントによると、基になるテーブルにトリガーが含まれるステートメントのOUTPUT句は、関数で定義された列に対してゼロのみを返します。INSERTINSTEAD OFIDENTITY(seed, increment)

この問題を回避するために、トリガーをトリガーとして書き直しましAFTERた。これにより、2 つのトリガーを 1 つのトリガーに統合することもできます。

CREATE TRIGGER [dbo].[CountryDivisions_Audit]
    ON [dbo].[CountryDivisions]
    AFTER INSERT, UPDATE
    AS
    BEGIN
        SET NoCount ON

        -- prevent recursion
        If TRIGGER_NESTLEVEL() > 1 RETURN

        UPDATE cdan
            SET
                [LastUpdatedBy] = USER_NAME()
                , [LastUpdatedDateTime] = GETUTCDATE()
            FROM
                [dbo].[CountryDivisions] AS cdan
                JOIN inserted AS i
                    LEFT JOIN deleted AS d
                        ON d.[CountryDivisionID] = i.[CountryDivisionID]
                    ON i.[CountryDivisionID] = cdan.[CountryDivisionID]
            WHERE
                -- only update records that have actually been updated.
                ISNULL(i.[CountryDivisionID], -1) <> ISNULL(d.[CountryDivisionID], -1)
                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.[DivisionType], '') <> ISNULL(d.[DivisionType], '')
                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
于 2016-08-09T16:21:52.177 に答える