MS IDENTITY_INSERT 设置为 OFF [重复]

Posted

技术标签:

【中文标题】MS IDENTITY_INSERT 设置为 OFF [重复]【英文标题】:MS IDENTITY_INSERT is set to OFF [duplicate] 【发布时间】:2018-05-27 17:18:31 【问题描述】:

我目前正在处理我的 SQL Server 项目。我遇到了这个错误,不知道如何解决。我检查了其他堆栈帖子,但它对我不起作用。我也试过 YouTube。我看了一段视频,并在我的插入语句之前和之后尝试了SET IDENTITY_INSERT (Table name) ON&SET IDENTITY_INSERT (Table name) OFF。我的代码是否有问题不允许我将数据插入到我的项目中?

CREATE TABLE Vet
(
    [VET_NUM] INT IDENTITY(1,1) CONSTRAINT Pk_Vet_VetNum PRIMARY KEY,
    [LAST_NAME] CHAR(20),
    [FIRST_NAME] CHAR(20),
    [STREET] CHAR(30),
    [CITY] CHAR(20),
    [STATE] CHAR(20),
    [POSTAL_CODE] CHAR(20),
    [SALARY] DECIMAL(8,2),
    [DEGREE] CHAR(20),
    [POSITION] CHAR(20)
);

CREATE TABLE Owner
(
    [OWNER_NUM] INT IDENTITY(1,1) CONSTRAINT Pk_Owner_OwnerNum PRIMARY KEY,
    [OWNER_NAME] CHAR(30) NOT NULL,
    [STREET] CHAR(30),
    [CITY] CHAR(20),
    [STATE] CHAR(20),
    [POSTAL_CODE] CHAR(20)
    --[PET_NUM] CHAR(5) -- Removed as duplicate (Owner Num already exists in Pet)
);

CREATE TABLE Pet
(
    [PET_NUM] INT IDENTITY(1,1) CONSTRAINT Pk_Pet_PetNum PRIMARY KEY,
    [PET_NAME] CHAR(35) NOT NULL,
    [STREET] CHAR(30),
    [CITY] CHAR(15),
    [STATE] CHAR(2),
    [POSTAL_CODE] CHAR(5),
    [BREED] CHAR(20),
    [OWNER_NUM] INT CONSTRAINT Fk_Pet_OwnerNum FOREIGN KEY REFERENCES Owner(OWNER_NUM),-- Added Foreign key reference 
    [APPOINTMENT_NUM] CHAR(5) 
)

CREATE TABLE Appointment
(
    [APPOINTMENT_NUM] INT IDENTITY(1,1) CONSTRAINT Pk_Appointment_AppointmentNum PRIMARY KEY,
    [APPOINTMENT_DATE] DATE,
    [VET_NUM] INT UNIQUE NOT NULL CONSTRAINT Fk_Appointment_VetNum FOREIGN KEY REFERENCES VET(VET_NUM),-- Added Foreign key reference  );
    [PET_NUM] INT UNIQUE NOT NULL CONSTRAINT Fk_Appointment_PetNum FOREIGN KEY REFERENCES Pet(PET_NUM)-- Added Foreign key reference  );
)

INSERT INTO vet ([vet_num],[last_name],[first_name],[street],[city],[state], [postal_code], [salary],[degree],[position]) 
VALUES (1, 'Skechley', 'Cristine', '24340 7th   Plaza', 'Pittsburgh', 'PA', '15274', 88053.30, 'Masters', 'Vet'), 
       (2, 'Fishpoole', 'Sig', '06784 Anthes Point', 'Philadelphia', 'PA', '19184', 45525.56, 'Associates', 'Receptionist'), 
       (3, 'Stother', 'Rycca', '04304 Superior Hill', 'Allentown', 'PA', '18105', 90553.37, 'Masters', 'Vet'), 
       (4, 'Scandrite', 'Kerrill', '30320 Express Crossing', 'Harrisburg', 'PA', '17126', 102553.59, 'Doctorates', 'Vet'), 
       (5, 'Glassman', 'Rhett', '33418 Tomscot Trail', 'Mc Keesport', 'PA', '15134', 56052.24, 'Bachelor', 'Management'), 
       (6, 'Gioan', 'Rab', '1 Pennsylvania Street', 'Hatfield', 'PA', '19440', 35880.76, 'Student', 'Janitor'), 
       (7, 'Patel', 'Dhruv', '24411 Jean Drive', 'Hatfield', 'PA', '19440', 42790.88, 'Student', 'Receptionist'), 
       (8, 'Smith', 'Giana', '24 Malple Street', 'Lansdale', 'PA', '19446', 35880.22, 'Student', 'Janitor'), 
       (9, 'Lopez', 'Briana', 'Orvilla', 'Allentown', 'PA', '18105', 74880.47, 'Masters', 'Vet'), 
       (10, 'Sam', 'Hector', 'Orvilla', 'Allentown', 'PA', '18105', 74880.47, 'Masters', 'Vet'); 

INSERT INTO owner ([owner_num],[owner_name],[street],[city],[state],[postal_code]) 
VALUES (11, 'Sammantha Rodgers','96372 Dexter Terrace','Erie', 'PA','16510'), 
       (12,'Jenkins Tim','486 Marcy Avenue','Philadelphia','PA','19184'), 
       (13,'Smith Bobby','03781 Meadow Ridge','Erie','PA','16510'), 
       (14,'Parker Aaron','80 Marcy Place','Harrisburg','PA','17126'), 
       (15,'Gil Malcomn','239 Tony Point','Harrisburg','PA','17110'), 
       (16,'Ramsey Torrey','0778 Columbus Park','Philadelphia','PA','19178'), 
       (17,'Novak Cole','63519 Warbler Way','Pittsburgh','PA','15261'), 
       (18,'Cunningham Dylan','0728 Esch Terrace','Pittsburgh ','PA','15274'), 
       (19,'Barclay Liam','36 John Wall Parkway','Pittsburgh ','PA','15210'), 
       (20,'Perez Joeseph','Hatfield Village','Hatfield','PA','19440');

INSERT INTO appointment ([APPOINTMENT_NUM],[appointment_date],[VET_NUM],[PET_NUM]) 
VALUES (21,'12/12/2017',1,25), 
       (30,'12/12/2017',2,26), 
       (23,'12/12/2017',1,27), 
       (29,'12/13/2017',4,23), 
       (25,'12/14/2017',5,22); 

INSERT INTO Pet ([PET_NUM], [PET_NAME], [STREET], [CITY], [STATE],[POSTAL_CODE], [BREED], [OWNER_NUM], [APPOINTMENT_NUM]) 
VALUES (21,'Jimmy','96372 Dexter Terrace','Erie', 'PA','16510','Affenpinscher',11,21), 
       (22,'Lily','486 Marcy Avenue','Philadelphia','PA','19184','American Bulldog',12,22), 
       (23,'Sally','03781 Meadow Ridge','Erie','PA','16510','American Eskimo Dog',13,23), 
       (24,'Joey','80 Marcy Place','Harrisburg','PA','17126','Barbet',14,24), 
       (25,'Rocky','239 Tony Point','Harrisburg','PA','17110','Papillon',15,25), 
       (26,'Sam','0778 Columbus Park','Philadelphia','PA','19178','McNab',16,26), 
       (27,'Chloe','63519 Warbler Way','Pittsburgh','PA','15261','Mountain Cur',17,27), 
       (28,'Mike','0728 Esch Terrace','Pittsburgh ','PA','15274','Pug',18,28), 
       (29,'Bruno','36 John Wall Parkway','Pittsburgh ','PA','15210','Pomeranian',19,9), 
       (30,'Daisy','Hatfield Village','Hatfield','PA','19440','Rat Terrier',20,30),
       (31,'Tim','Hatfield Village','Hatfield','PA','19440','German Shepherd',20,30);  

我收到的错误消息:

消息 544,第 16 级,状态 1,第 53 行 当 IDENTITY_INSERT 设置为 OFF 时,无法为表“Vet”中的标识列插入显式值。

消息 544,第 16 级,状态 1,第 68 行 当 IDENTITY_INSERT 设置为 OFF 时,无法在表“所有者”中插入标识列的显式值。

消息 544,第 16 层,状态 1,第 81 行 当 IDENTITY_INSERT 设置为 OFF 时,无法在表“约会”中插入标识列的显式值。

消息 544,第 16 级,状态 1,第 89 行 当 IDENTITY_INSERT 设置为 OFF 时,无法为表“Pet”中的标识列插入显式值。

【问题讨论】:

您在代码中的任何地方都没有使用SET IDENTITY INSERT ON,但是,您正试图向标识列显式插入一个值 您还遇到了AppointmentPet 插入数据的问题。因为您在每个表中引用另一个表,您将无法插入数据。您正在尝试插入 Appointment 数据而没有任何 Pet 数据。为什么Pet 表上有Appointment_Num @WEI_DBA 很好看,没有那个专栏的意义 【参考方案1】:

您必须在运行其余代码的同一会话中设置 IDENTITY_INSERT。在为另一个表打开它之前,您还必须将 IDENTITY_INSERT 设置为 OFF。像这样的:

SET IDENTITY_INSERT owner ON

INSERT INTO owner 
           ([owner_num],[owner_name],[street],[city],[state],[postal_code]) 
VALUES     (11,'Sammantha Rodgers','96372 Dexter Terrace','Erie', 'PA','16510'), 
           (12,'Jenkins Tim','486 Marcy Avenue','Philadelphia','PA','19184'), 
           (13,'Smith Bobby','03781 Meadow Ridge','Erie','PA','16510'), 
           (14,'Parker Aaron','80 Marcy Place','Harrisburg','PA','17126'), 
           (15,'Gil Malcomn','239 Tony Point','Harrisburg','PA','17110'), 
           (16,'Ramsey Torrey','0778 Columbus Park','Philadelphia','PA','19178'), 
           (17,'Novak Cole','63519 Warbler Way','Pittsburgh','PA','15261'), 
           (18,'Cunningham Dylan','0728 Esch Terrace','Pittsburgh ','PA','15274'), 
           (19,'Barclay Liam','36 John Wall Parkway','Pittsburgh ','PA','15210'), 
           (20,'Perez Joeseph','Hatfield Village','Hatfield','PA','19440');
SET IDENTITY_INSERT owner OFF
SET IDENTITY_INSERT appointment ON
INSERT INTO appointment 
           ([APPOINTMENT_NUM],[appointment_date],[VET_NUM],[PET_NUM]) 
VALUES     (21,'12/12/2017',1,25), 
           (30,'12/12/2017',2,26), 
           (23,'12/12/2017',1,27), 
           (29,'12/13/2017',4,23), 
           (25,'12/14/2017',5,22); 
SET IDENTITY_INSERT appointment OFF
SET IDENTITY_INSERT Pet ON
INSERT INTO Pet 
           ([PET_NUM],[PET_NAME],[STREET],[CITY],[STATE],[POSTAL_CODE],
           [BREED],[OWNER_NUM],[APPOINTMENT_NUM]) 
VALUES     (21,'Jimmy','96372 Dexter Terrace','Erie', 'PA','16510','Affenpinscher',11,21), 
           (22,'Lily','486 Marcy Avenue','Philadelphia','PA','19184','American Bulldog',12,22), 
           (23,'Sally','03781 Meadow Ridge','Erie','PA','16510','American Eskimo Dog',13,23), 
           (24,'Joey','80 Marcy Place','Harrisburg','PA','17126','Barbet',14,24), 
           (25,'Rocky','239 Tony Point','Harrisburg','PA','17110','Papillon',15,25), 
           (26,'Sam','0778 Columbus Park','Philadelphia','PA','19178','McNab',16,26), 
           (27,'Chloe','63519 Warbler Way','Pittsburgh','PA','15261','Mountain Cur',17,27), 
           (28,'Mike','0728 Esch Terrace','Pittsburgh ','PA','15274','Pug',18,28), 
           (29,'Bruno','36 John Wall Parkway','Pittsburgh ','PA','15210','Pomeranian',19,29), 
           (30,'Daisy','Hatfield Village','Hatfield','PA','19440','Rat Terrier',20,30),
           (31,'Tim','Hatfield Village','Hatfield','PA','19440','German Shepherd',20,30);  

【讨论】:

【参考方案2】:

请设置

之前设置 IDENTITY_INSERT ON 并在每个插入语句之后设置 IDENTITY_INSERT OFF。

问候 阿卜杜勒

【讨论】:

以上是关于MS IDENTITY_INSERT 设置为 OFF [重复]的主要内容,如果未能解决你的问题,请参考以下文章

无法插入显式值,因为 IDENTITY_INSERT 为 OFF,但无法将 IDENTITY_INSERT 设置为 ON,因为它已经为 ON

IDENTITY_INSERT 设置为 OFF 的问题? :-/

强制 SET IDENTITY_INSERT 从 MS Access 更快生效

IDENTITY_INSERT 设置为 OFF?

错误 IDENTITY_INSERT 设置为 OFF 但实际上它是 ON

IDENTITY_INSERT 设置为 OFF - 如何将其打开?