獲取以下錯誤:(第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列,但無法讓它工作 –