gpt4 book ai didi

sql-server - 标识列的显式值?

转载 作者:行者123 更新时间:2023-12-05 01:48:36 25 4
gpt4 key购买 nike

我正在尝试备份我的第一个表 DocumentManagement.tbDocumentsHistory 中的数据并将其保存在 DocumentManagement.tbDocuments 上

并且由于 DocumentManagement.tbDocuments 具有 ID IDENTITY_INSERT 列,因此将引发此错误

An explicit value for the identity column in table 'DocumentManagement.tbDocuments' can only be specified when a column list is used and IDENTITY_INSERT is ON.

当我使用这段代码时

DELETE FROM DocumentManagement.tbDocumentsHistory
OUTPUT DELETED.* INTO DocumentManagement.tbDocuments
FROM DocumentManagement.tbDocumentsHistory
WHERE DocumentID=@DocumentID

我尝试使用此代码关闭 DocumentManagement.tbDocuments 上的 IDENTITY_INSERT

SET IDENTITY_INSERT DocumentManagement.tbDocuments ON 
DELETE FROM DocumentManagement.tbDocumentsHistory
OUTPUT DELETED.* INTO DocumentManagement.tbDocuments
FROM DocumentManagement.tbDocumentsHistory
WHERE DocumentID=@DocumentID
SET IDENTITY_INSERT DocumentManagement.tbDocuments OFF

我仍然面临同样的错误!

我的表格描述:

CREATE TABLE [DocumentManagement].[tbDocuments](
[DocumentID] [bigint] IDENTITY(1,1) NOT NULL,
[DocumentNameEn] [nvarchar](max) NULL,
[DocumentNameAr] [nvarchar](max) NULL,
[DocumentDescriptionEn] [nvarchar](max) NULL,
[DocumentDescriptionAr] [nvarchar](max) NULL,
[CreatedOn] [datetime2](7) NULL,
[ModifiedOn] [datetime2](7) NULL,
[AddedBy] [bigint] NULL,
[modifaiedBy] [bigint] NULL,
[PhysicalName] [nvarchar](max) NULL,
[Extension] [nvarchar](15) NULL,
[DocumentTypeID] [bigint] NULL,
[PhysicalPath] [nvarchar](max) NULL,


CONSTRAINT [PK_tbDocuments] PRIMARY KEY CLUSTERED([DocumentID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) 在[主要]上

CREATE TABLE [DocumentManagement].[tbDocumentsHistory](
[DocumentID] [bigint] NOT NULL,
[DocumentNameEn] [nvarchar](max) NULL,
[DocumentNameAr] [nvarchar](max) NULL,
[DocumentDescriptionEn] [nvarchar](max) NULL,
[DocumentDescriptionAr] [nvarchar](max) NULL,
[CreatedOn] [datetime2](7) NULL,
[ModifiedOn] [datetime2](7) NULL,
[AddedBy] [bigint] NULL,
[modifaiedBy] [bigint] NULL,
[PhysicalName] [nvarchar](max) NULL,
[Extension] [nvarchar](15) NULL,
[DocumentTypeID] [bigint] NULL,
[PhysicalPath] [nvarchar](max) NULL, CONSTRAINT [PK_tbDocumentsHistory] PRIMARY KEY CLUSTERED([DocumentID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

最佳答案

您需要修改您的 INSERT 语句并添加一个包含标识列名称的列列表才能使其正常工作。

任何使用标识插入的 INSERT 语句都需要一个列列表,如下所示:

CREATE TABLE dbo.Tab 
(
ID INT IDENTITY NOT NULL PRIMARY KEY,
Name VARCHAR(40) NOT NULL
)

SET IDENTITY_INSERT dbo.Tab ON
GO

INSERT INTO dbo.Tab (ID, Name) VALUES (3000, 'Groucho')
GO

SET IDENTITY_INSERT dbo.Tab OFF

关于sql-server - 标识列的显式值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9653544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com