简体   繁体   English

复制时插入带有标识列的表会导致 SQL Server 出错

[英]Inserting into table with an Identity column while replication causes error in SQL Server

I have a table A_tbl in my database.我的数据库中有一个表A_tbl I have created a trigger on A_tbl to capture inserted records.我在A_tbl上创建了一个触发器来捕获插入的记录。 Trigger is inserting records in my queue table B_tbl .触发器正在我的队列表B_tbl插入记录。 This table has an Identity column with property "Not for replication" as 1.此表有一个Identity列,其属性“不用于复制”为 1。

  • A_tbl (Id, name, value) with Id as the primary key A_tbl (Id, name, value) 以Id为主键
  • B_tbl (uniqueId, Id) with uniqueId as Identity column B_tbl ( B_tbl , Id) 以uniqueId作为Identity

Trigger code doing this:执行此操作的触发代码:

Insert into B_tbl (Id)
    select i.Id from inserted

Now my table 'B' is replicated to another DB Server, now when I'm inserting into table 'A' it is causing this error:现在我的表 'B' 被复制到另一个数据库服务器,现在当我插入表 'A' 时,它导致了这个错误:

Explicit value must be specified for identity column in table 'B_tbl' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.当 IDENTITY_INSERT 设置为 ON 或复制用户插入 NOT FOR REPLICATION 标识列时,必须为表 'B_tbl' 中的标识列指定显式值。 (Source: MSSQLServer, Error number: 545) (来源:MSSQLServer,错误号:545)

Please help me resolve this issue.请帮我解决这个问题。

You have to do something like this 你必须做这样的事情

SET IDENTITY_INSERT A_tbl  ON

Insert into B_tbl (uniqueid, Id)
select 1, i.id from inserted

SET IDENTITY_INSERT A_tbl  OFF

The trigger code should contain the Identity insert ON option as below 触发器代码应包含Identity insert ON选项,如下所示

SET IDENTITY_INSERT B_tbl ON

Insert into B_tbl (uniqueid,Id)
select identityvalue,i.Id from inserted

SET IDENTITY_INSERT B_tbl OFF

There are basically 2 different ways to INSERT records without having an error: 在没有错误的情况下,INSERT记录基本上有两种不同的方法:

1) When the IDENTITY_INSERT is set OFF. 1)当IDENTITY_INSERT设置为OFF时。 The PRIMARY KEY "ID" MUST NOT BE PRESENT 主要钥匙“身份证”不得存在

2) When the IDENTITY_INSERT is set ON. 2)当IDENTITY_INSERT设置为ON时。 The PRIMARY KEY "ID" MUST BE PRESENT 必须存在 PRIMARY KEY “ID”

As per the following example from the same Table created with an IDENTITY PRIMARY KEY: 根据使用IDENTITY PRIMARY KEY创建的同一个表中的以下示例:

CREATE TABLE [dbo].[Persons] (    
    ID INT IDENTITY(1,1) PRIMARY KEY,
    LastName VARCHAR(40) NOT NULL,
    FirstName VARCHAR(40)
);

1) In the first example, you can insert new records into the table without getting an error when the IDENTITY_INSERT is OFF. 1)在第一个示例中,您可以在IDENTITY_INSERT为OFF时将新记录插入表中而不会出现错误。 The PRIMARY KEY "ID" MUST NOT BE PRESENT from the "INSERT INTO" Statements and a unique ID value will be added automatically: . “INSERT INTO”语句中不得出现 PRIMARY KEY “ID”, 并自动添加唯一的ID值: If the ID is present from the INSERT in this case, you will get the error "Cannot insert explicit value for identify column in table..." 如果在这种情况下INSERT中存在ID,您将收到错误“无法为表中的标识列插入显式值...”

SET IDENTITY_INSERT [dbo].[Persons] OFF;
INSERT INTO [dbo].[Persons] (FirstName,LastName)
VALUES ('JANE','DOE'); 
INSERT INTO Persons (FirstName,LastName) 
VALUES ('JOE','BROWN');

OUTPUT of TABLE [dbo].[Persons] will be: 表[dbo]的输出。[人员]将是:

ID    LastName   FirstName
1     DOE        Jane
2     BROWN      JOE

2) In the Second example, you can insert new records into the table without getting an error when the IDENTITY_INSERT is ON. 2)在第二个示例中,您可以在IDENTITY_INSERT为ON时将新记录插入表中而不会出现错误。 The PRIMARY KEY "ID" MUST BE PRESENT from the "INSERT INTO" Statements as long as the ID value does not already exist : If the ID is NOT present from the INSERT in this case, you will get the error "Explicit value must be specified for identity column table..." 只要ID值不存在 ,PRIMARY KEY “ID”必须出现在“INSERT INTO”语句 :如果在这种情况下INSERT中不存在ID,则会出现“显式值必须为为标识列表指定...“

SET IDENTITY_INSERT [dbo].[Persons] ON;
INSERT INTO [dbo].[Persons] (ID,FirstName,LastName)
VALUES (5,'JOHN','WHITE'); 
INSERT INTO [dbo].[Persons] (ID,FirstName,LastName)
VALUES (3,'JACK','BLACK'); 

OUTPUT of TABLE [dbo].[Persons] will be: 表[dbo]的输出。[人员]将是:

ID    LastName   FirstName
1     DOE        Jane
2     BROWN      JOE
3     BLACK      JACK
5     WHITE      JOHN

Just to add one possible gotcha for people who like me did everything by the book and still got the message.只是为像我这样的人添加一个可能的问题,按照本书做了所有事情并且仍然收到消息。

Check for triggers on insert.检查插入时的触发器。 They may be attempting to create more rows while executing code that does not have the explicit identity column values.他们可能会在执行没有显式标识列值的代码时尝试创建更多行。

If it's the case, disable the trigger (or drop and recreate it).如果是这种情况,请禁用触发器(或删除并重新创建它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM