0

我正在开发一个存储过程以将条目批量添加到表中。

这是我到目前为止所得到的:

use [DebugDatabase]
go

set ansi_nulls on
go

set quoted_identifier on
go

create procedure [dbo].[AddMultipleErrors]
    @errors ErrorTableType readonly

as

declare @toInsert ErrorTableType

begin
insert into @toInsert select * from @errors
insert into [dbo].[Error] (ErrorMessage, FileId) select ErrorMessage, FileId from @toInsert
end
go

我收到消息“只有在使用列列表并且 IDENTITY_INSERT 为 ON 时,才能指定表 '@toInsert' 中标识列的显式值。”

我尝试在“declare”语句之后添加“set identity_insert @toInsert on”行,但出现“语法错误”错误。我已经尝试将它移动到其他几个位置,结果相同。

现在,我做了一些研究,看起来我实际上并不想将“identity_insert”设置为 on,因为它会要求我指定一个 id 而不是让表自动生成一个,这就是我确实想。

假设,我需要做的就是在插入调用中指定表中的每个单独的列,省略 id 值,并且一切都应该匹配得很好。但是,除非我弄错了,程序已经这样设置了,但它仍然不喜欢它。

有什么我可能会丢失的吗?

4

2 回答 2

1

您的整个过程可以大大简化到此。

create procedure [dbo].[AddMultipleErrors]
    @errors ErrorTableType readonly
as

insert into [dbo].[Error] (ErrorMessage, FileId) 
select ErrorMessage, FileId from @errors

go
于 2014-12-10T20:02:35.233 回答
0

好吧,这就是我们想出的:

我收到有关 ID 的错误的原因不是因为插入过程,而是因为表类型。在 ErrorTableType 中,我将 Id 与其他列一起包括在内。从表类型中删除 Id(但将其保留在表本身中)似乎可以修复错误。

于 2014-12-10T20:03:59.127 回答