2012-03-11 51 views
2

我想使我的數據備份從我的第一臺DocumentManagement.tbDocumentsHistory,並將其保存在DocumentManagement.tbDocuments標識列的顯式值?

而且由於DocumentManagement.tbDocuments有ID IDENTITY_INSERT欄這個錯誤會引發

表'DocumentManagement.tbDocuments'中的標識列的顯式值只能在使用列列表且IDENTITY_INSERT爲ON時指定。

當我用這個代碼

DELETE FROM DocumentManagement.tbDocumentsHistory 
OUTPUT DELETED.* INTO DocumentManagement.tbDocuments 
FROM DocumentManagement.tbDocumentsHistory 
WHERE [email protected] 

我試圖關閉IDENTITY_INSERT上DocumentManagement.tbDocuments使用此代碼

SET IDENTITY_INSERT DocumentManagement.tbDocuments ON 
     DELETE FROM DocumentManagement.tbDocumentsHistory 
      OUTPUT DELETED.* INTO DocumentManagement.tbDocuments 
      FROM DocumentManagement.tbDocumentsHistory 
     WHERE [email protected] 
     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] 

)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

使文檔名稱的nvarchar(max)。真的嗎? – 2012-03-11 08:52:30

+0

它只是一個測試數據! – HAJJAJ 2012-03-11 09:02:31

回答

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 [email protected] 
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 
+0

我完全不明白你的觀點,請你澄清一下。 – HAJJAJ 2012-03-11 08:47:14

+0

不要使用*進行插入,明確指定列名稱 – kaj 2012-03-11 08:55:52

+0

@KAJ:我認爲你發佈的鏈接狀態ID確實如此。 – Mithrandir 2012-03-11 08:58:32

相關問題