The target table '%.*ls' of the OUTPUT INTO clause cannot be on either side of a (primary key, foreign key) relationship. Found reference constraint '%ls'.

Error Message:
Msg 332, Level 16, State 1, Line 6
The target table ‘%.*ls’ of the OUTPUT INTO clause cannot be on either side of a (primary key, foreign key) relationship. Found reference constraint ‘%ls’.

Severity level:
16.

Description:
This error message appears when you try to use the OUTPUT INTO clause for a target table that appears on either side of a PRIMARY KEY – FOREIGN KEY relationship.

Consequences:
The T-SQL statement can be parsed, but causes the error at runtime.

Resolution:
Errors of the Severity Level 16 are generated by the user and can be fixed by the SQL Server user. The statement cannot be executed this way. The target table of the OUTPUT INTO clause cannot be on either side of a PRIMARY KEY – FOREIGN KEY relationship.

Versions:
This error message was introduced with SQL Server 2005.

Example(s):
IF OBJECT_ID (‘dbo.t’) IS NOT NULL
    DROP TABLE dbo.t;
IF OBJECT_ID (‘dbo.t2’) IS NOT NULL
    DROP TABLE dbo.t2;
IF OBJECT_ID (‘dbo.t3’) IS NOT NULL
    DROP TABLE dbo.t3;

CREATE TABLE dbo.t
    (c1 int,
    s1 varchar(20));

GO
CREATE TABLE dbo.t2
    (c1 int PRIMARY KEY,
    s1 varchar(20));
GO

CREATE TABLE dbo.t3
    (c1 int
    CONSTRAINT FK_t3_t2 FOREIGN KEY (c1) REFERENCES dbo.t2(c1));

INSERT INTO dbo.t
    OUTPUT
        inserted.c1,
        inserted.s1
    INTO
        dbo.t2
SELECT 1, ‘a’;

Remarks:
In the above example we try to insert data into the table dbo.t2 via the OUTPUT INTO clause. Because dbo.t2 appears on the PRIMARY KEY side of a PRIMARY KEY – FOREIGN KEY relation, the error is raised.

]]>

Leave a comment

Your email address will not be published.