The documentation states that the output table may not participate in foreign key constraints (or check constraints, and also not allow triggers defined on it), but these cases are not considered here). Msg 332 error is a manifestation of this.
( THIS IS NOT RECOMMENDED. See update below)
However, I found that this restriction is undermined if the foreign key constraint is disabled and reenabled with NOCHECK/CHECK even after re-enabling the constraint. In other words: it is enough to have one cycle with the switch off, so that the FK restriction is "invisible" to prohibit output to a foreign key. Pay attention to the modification below:
IF OBJECT_ID ('dbo.forn') IS NOT NULL begin alter table dbo.forn drop constraint FK_forn_prim DROP TABLE dbo.forn; end IF OBJECT_ID ('dbo.prim') IS NOT NULL DROP TABLE dbo.prim; go CREATE TABLE dbo.prim (c1 int PRIMARY KEY);
The following errors (violation of restrictions), so you know that DRI is still working:
INSERT INTO dbo.prim OUTPUT inserted.c1 + 1 INTO dbo.forn select 2;
Given the ban on the documents, I give it seems dubious. I have a question awaiting an answer to this question, although it was not well received.
UPDATE
The previously mentioned question has been answered , and new information will invalidate this answer. Leaving this as a warning to others, in case they stumble upon this seemingly random hole.
Result: alter TABLE dbo.forn check CONSTRAINT FK_forn_prim ; re-fixes the constraint, but leaves it in an “unreliable" state, so it cannot be fully used by the SQL engine to optimize the index, etc. This is not recommended. The right way to reuse is
alter table dbo.forn with check check CONSTRAINT FK_forn_prim;
source share