获取以下错误:(第21行是声明语句)。标识插入 - 错误消息
Msg 544, Level 16, State 1, Procedure insert_employee_details, Line 21
Cannot insert explicit value for identity column in table 'DB_Actions' when IDENTITY_INSERT is set to OFF.
这是我曾尝试:
SET IDENTITY_INSERT DB_Actions ON;
但后来我得到这个错误:
Msg 8107, Level 16, State 1, Line 31
IDENTITY_INSERT is already ON for table 'StacysDB.dbo.Employee_Details'. Cannot perform SET operation for table 'DB_Actions'.
这不会使得感知到我。首先它不起作用,因为身份插入关闭。然后,当我尝试打开它时,它已经打开了。
我知道这是一个常见的错误,所以我尝试了一个确认的解决方案,在插入值之前重新创建表并设置标识插入,然后返回并插入值,但我得到了相同的错误(Msg 8107)。
感谢您的任何帮助。
--1
--CREATE TABLE DB_Actions
--(
--Id numeric(5,0) IDENTITY(1,1) PRIMARY KEY,
--Table_Name varchar(20),
--Action_Name varchar(10),
--User_Name varchar(50),
--Done_ON datetime,
--Record_Id numeric(5,0)
--);
--2
--INSERT TRIGGER
--CREATE TRIGGER insert_employee_details
--ON Employee_Details
--FOR INSERT
--AS
--DECLARE @id int, @name varchar(20)
--SELECT @id = Emp_Id, @name = Emp_First_Name FROM inserted
--INSERT INTO DB_Actions(Id, Table_Name, Action_Name, User_Name,
--Done_ON, Record_Id)
--VALUES(@id,
-- 'Employee_Details',
-- 'INSERT',
-- @name,
-- getdate(),
-- @id
--);
INSERT INTO Employee_Details(Emp_Id, Emp_First_Name, Emp_Middle_Name, Emp_Last_Name, Emp_Address1, Emp_Address2, Emp_Country_Id, Emp_State_Id, Emp_City_Id, Emp_Zip, Emp_Mobile, Emp_Gender, Desig_Id, Emp_DOB, Emp_JoinDate, Emp_Active)
VALUES(9000, 'A', 'B', 'C', 'D', 'E', 2, 3, 4, 44444, 4444444, 1, 3333, getdate(), getdate(), 0);
我也读过断开和重新连接可以工作。但事实并非如此。 –
在这里稍微退一步,你是否真的需要'Id'作为一个'IDENTITY'列呢?它看起来像某种类型的审计表,为什么不把它变成一个普通的“INT”列呢? – DavidG
它需要是身份和主键。我试图删除我的插入声明中的Id列,但无法让它工作 –