是否可以将类型为“table”的参数以及类型为“[int] IDENTITY(1,1)”的列传递给过程,并使用作为输入参数传递的 DataTable 对象来执行此存储过程?
我收到以下错误:“插入表变量不允许的标识列。
表值参数“@xxxxx”的数据不符合参数的表类型。”
我能找到的唯一相关评论是“如果您为表中的标识列提供值-值参数,您必须为会话发出 SET IDENTITY_INSERT 语句。”
似乎即使未在表参数中设置 PK,它也会在某个时刻自动设置。这种情况在哪里发生以及如何避免?
Is it possible to pass a parameter of type "table" with a column of type "[int] IDENTITY(1,1)" to a procedure and execute this stored procedure with a DataTable object passed as the input parameter?
I get the following error: "INSERT into an identity column not allowed on table variables.
The data for table-valued parameter \"@xxxxx\" doesn't conform to the table type of the parameter."
The only related comment I was able to find was "If you supply a value for an identity column in a table-valued parameter, you must issue the SET IDENTITY_INSERT statement for the session."
It seems that even though the PK was not set in the table parameter, it gets set automatically at some point. Where does that happen and how can it be avoided?
发布评论
评论(2)
我遇到了同样的问题,我们想要类型的标识,但不想提供值。关键是为该列使用
SqlMetaData
构造函数,将useServerDefault
设置为true
:根据 在 ado net 中使用带有标识列的用户定义表类型,作者:Tim Van Wassenhove
SQL:
C#:
I had this same problem where we want an identity on the type, but don't want to provide a value. The key is to use a
SqlMetaData
constructor for that column that setsuseServerDefault
totrue
:According to this article on using user defined table type with identify column in ado net by Tim Van Wassenhove
SQL:
C#:
如果问题是“如何传递带有标记为 INT IDENTITY(1,1) 的列的 TVP?”你不知道。 TVP 不是一张桌子。您可能指定了 TVP 值,这意味着您不希望所提供的值具有标识。
该表通常是您想要声明 IDENTITY 的表。
如果问题是“如何避免在TVP中设置PK?”那么我就不得不要求更多的代码。
If the question is "how do I pass a TVP with a column marked INT IDENTITY(1,1)?" you don't. A TVP is not a table. You may be specifying the TVP values, which means that you don't want an identity on the value that you're supplying.
The table is usually the one that you want to have the IDENTITY declared on.
If the question is "How can I avoid setting the PK in the TVP?" then I would have to ask for more code.