Is there a way to set IDENTITY_INSERT ON
for table valued type?
TL;DR: No.
SET IDENTITY_INSERT
is a command to be used against a table object, not a variable. SET IDENTITY_INSERT (Transact-SQL):
SET IDENTITY_INSERT (Transact-SQL)
Allows explicit values to be inserted into the identity column of a table.
##Syntax
SET IDENTITY_INSERT [ [ database_name . ] schema_name . ] table_name { ON | OFF }
Arguments
database_name
Is the name of the database in which the specified table resides.
schema_name
Is the name of the schema to which the table belongs.
table_name
Is the name of a table with an identity column.
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 an IDENTITY
property and the other without:
CREATE TYPE dbo.tvp_test_i AS TABLE (id INT NOT NULL IDENTITY(1, 1),
a INT NULL);
CREATE TYPE dbo.tvp_test_ni AS TABLE (id INT NOT NULL,
a INT NULL);
GO
DECLARE @i dbo.tvp_test_i;
INSERT INTO @i (a)
VALUES(17),(21);
DECLARE @ni dbo.tvp_test_ni;
INSERT INTO @ni (id,a)
VALUES(3,95),(5,34);
SELECT *
FROM @i;
SELECT *
FROM @ni;