我正在尝试从我的第一个表DocumentManagement.tbDocumentsHistory备份我的数据并将其保存在DocumentManagement.tbDocuments
并且因为DocumentManagement.tbDocuments具有ID IDENTITY_INSERT列,所以会出现此错误
只有在使用列列表并且IDENTITY_INSERT为ON时,才能指定表'DocumentManagement.tbDocuments'中标识列的显式值。
当我使用此代码时
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
我仍然面临同样的错误!
我的桌子desc:
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]
)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]
答案 0 :(得分:3)
这将解决问题,但我试图使用OUTPUT
做得更好SET IDENTITY_INSERT DocumentManagement.tbDocuments ON
INSERT INTO DocumentManagement.tbDocuments
( DocumentID, DocumentNameEn , DocumentNameAr , DocumentDescriptionEn ,
DocumentDescriptionAr , CreatedOn , ModifiedOn ,
AddedBy , modifaiedBy , PhysicalName ,Extension ,
DocumentTypeID ,PhysicalPath
)
SELECT * FROM DocumentManagement.tbDocumentsHistory
SET IDENTITY_INSERT DocumentManagement.tbDocuments OFF
DELETE FROM DocumentManagement.tbDocumentsHistory
FROM DocumentManagement.tbDocumentsHistory
WHERE DocumentID=@DocumentID
答案 1 :(得分:2)
您需要修改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
答案 2 :(得分:0)
TL; DR::错误所请求的列列表说明是目标表的列列表。在这种情况下,OUTPUT DELETED.* INTO DestinationTable (IdentityColumn, othercolumn, anothercolumn,...)
,如果您仅从源数据中显式指示列而不是进行Deleted.*
,则将无法使用。
错误消息指出您必须打开标识插入并指定列列表。问题在于您尚未为插入内容指定列列表。
您可以签出output clause documentation来查看如何在此处指示列列表。在这种情况下,考虑到您自己接受的答案上的列,您的sql代码将类似于:
DELETE FROM DocumentManagement.tbDocumentsHistory
OUTPUT DELETED.DocumentID, DELETED.DocumentNameEn, Deleted.DocumentNameAr,
Deleted.DocumentDescriptionEn, Deleted.DocumentDescriptionAr, Deleted.CreatedOn,
Deleted.ModifiedOn, Deleted.AddedBy, Deleted.modifaiedBy, Deleted.PhysicalName,
Deleted.Extension, Deleted.DocumentTypeID, Deleted.PhysicalPath INTO DocumentManagement.tbDocuments
(
DocumentID, DocumentNameEn , DocumentNameAr , DocumentDescriptionEn ,
DocumentDescriptionAr , CreatedOn , ModifiedOn ,
AddedBy , modifaiedBy , PhysicalName ,Extension ,
DocumentTypeID ,PhysicalPath
)
FROM DocumentManagement.tbDocumentsHistory
WHERE DocumentID=@DocumentID