Is there a way to set IDENTITY_INSERT ON for table valued type? The way how it is done with tables - isn't working.
CREATE TYPE dbo.tvp_test AS TABLE
(
id INT NOT NULL IDENTITY(1, 1),
a INT NULL
);
GO
DECLARE @test dbo.tvp_test;
SET IDENTITY_INSERT @test ON;
INSERT INTO @test VALUES (1, 1);
DROP TYPE dbo.tvp_test;
Error:
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near '@test'
TL;DR: No.
SET IDENTITY_INSERTis a command to be used against a table object, not a variable. SET IDENTITY_INSERT (Transact-SQL):Notice that this makes no reference to a variable at all; that's because it can't be used against one.
If you do need two versions of a Table Type, one that allows explicit values of its ID column, and the other that uses an
IDENTITY, you will need to define 2 table types; one with anIDENTITYproperty and the other without: