IDENTITY_INSERT与sql有关

时间:2017-08-14 21:54:06

标签: sql-server azure

当我运行查询时,我正在尝试填充azure数据库:

SET IDENTITY_INSERT author ON

DECLARE @TEASER varchar(4000) = 'text..';
DECLARE @BODY varchar(4000) = 'text...';

insert into author(id,first_name,last_name,email) values (1,'Dan','Vega','danvega@gmail.com');
insert into author(id,first_name,last_name,email) values (2,'John','Smith','johnsmith@gmail.com');

insert into post(id,title,slug,teaser,body,author_id,posted_on) values (1,'Spring Boot Rocks!','spring-boot-rocks',@TEASER,@BODY,1,DEFAULT);

我在插入帖子行时出错:Cannot insert explicit value for identity column in table 'post' when IDENTITY_INSERT is set to OFF.运行后,作者会被创建,但不会发布帖子。

如果我添加像 SET IDENTITY_INSERT作者ON SET IDENTITY_INSERT Post ON 我有IDENTITY_INSERT is already ON for table 'spring-boot-intro.dbo.author'. Cannot perform SET operation for table 'Post'。 。如果我只添加 SET IDENTITY_INSERT帖子,我有Cannot insert explicit value for identity column in table 'author' when IDENTITY_INSERT is set to OFF

2 个答案:

答案 0 :(得分:0)

  

任何时候,会话中只有一个表可以将IDENTITY_INSERT属性设置为ON。

来自docs

答案 1 :(得分:0)

我设法通过明确打开和关闭IDENTITY_INSERT

来解决这个问题
SET IDENTITY_INSERT author ON
*author insertions here*
SET IDENTITY_INSERT author OFF
SET IDENTITY_INSERT post ON
*post insertions here*