Bạn phải sử dụng SET IDENTITY_INSERT TO ON. Nếu bạn đặt nó là ON thì bạn nên chuyển giá trị một cách rõ ràng tới cột ID.
Tại sao bạn nên tắt Nhận dạng? Có thể bạn đang cố gắng chuyển một số giá trị rõ ràng.
Vui lòng tham khảo bản trình diễn mẫu tại đây.
-- Create tool table.
CREATE TABLE dbo.Tool
(
ID INT IDENTITY NOT NULL PRIMARY KEY,
NAME VARCHAR(40) NOT NULL
);
GO
-- Inserting values into products table.
INSERT INTO dbo.Tool
(NAME)
VALUES ('Screwdriver'),
('Hammer'),
('Saw'),
('Shovel');
GO
-- Create a gap in the identity values.
DELETE dbo.Tool
WHERE NAME = 'Saw';
GO
SELECT *
FROM dbo.Tool;
GO
-- Try to insert an explicit ID value of 3;
-- should return a warning.
INSERT INTO dbo.Tool
(ID,
NAME)
VALUES (3,
'Garden shovel');
GO
-- SET IDENTITY_INSERT to ON.
SET IDENTITY_INSERT dbo.Tool ON;
GO
-- Try to insert an explicit ID value of 3.
INSERT INTO dbo.Tool
(ID,
NAME)
VALUES (3,
'Garden shovel');
GO
SELECT *
FROM dbo.Tool;
GO
-- Drop products table.
DROP TABLE dbo.Tool;
GO