2009-09-16 136 views
0

我正在將數據從舊數據庫傳輸到新數據庫,並且在將數據插入新數據庫時需要保留舊數據庫的標識。但是,它不起作用。將行插入到具有預定義標識的表中

這是我的查詢:

SET IDENTITY_INSERT RentalEase.dbo.tblTenant ON 
INSERT INTO RentalEase.dbo.tblTenant SELECT [ID] 
    ,1 
     ,[PropertyID] 
    ,0 
     ,[UnitID] 
     ,[TenantName] 
     ,[Sex] 
     ,[BirthDate] 
     ,[SSNO] 
     ,[CoTenant1] 
     ,[CoTenant1Sex] 
     ,[CoTenant1BirthDate] 
     ,[CoTenant1SSNO] 
     ,[CoTenant2] 
     ,[CoTenant2Sex] 
     ,[CoTenant2BirthDate] 
     ,[CoTenant2SSNO] 
     ,[CoTenant3] 
     ,[CoTenant3Sex] 
     ,[CoTenant3BirthDate] 
     ,[CoTenant3SSNO] 
     ,[CarColor] 
     ,[CarModel] 
     ,[CarYear] 
     ,[CarState] 
     ,[CarPlateNumber] 
     ,[Memo] 
     ,[Address1] 
     ,[Address2] 
     ,[Address3] 
     ,[Address4] 
     ,[Phone] 
     ,[ReferBy] 
     ,[BeginDate] 
     ,[NoticeGiven] 
     ,[LeaseMonth2Month] 
     ,[LeaseEnds] 
     ,[DepositPaid] 
     ,[DepositRefundable] 
     ,[RefundMemo] 
     ,[RentDueDay] 
     ,[Charge1] 
     ,[Charge1Amount] 
     ,[Charge2] 
     ,[Charge2Amount] 
     ,[Charge3] 
     ,[Charge3Amount] 
     ,[Charge4] 
     ,[Charge4Amount] 
     ,[ContractText] 
     ,[BalanceDue] 
    FROM [oldTables].[dbo].[tblCurrentTenant] 

    SET IDENTITY_INSERT RentalEase.dbo.tblTenant OFF 

的SQL Server抱怨說,「當使用列列表只能指定表RentalEase.dbo.tblTenant」的標識列一個明確的價值和IDENTITY_INSERT爲ON 「。

我需要做些什麼才能使它起作用?

回答

6

「當一個列列表用於和IDENTITY_INSERT爲ON用於表RentalEase.dbo.tblTenant」 標識列的顯式值只能指定」。

因此,使用列的列表,如消息指出:

SET IDENTITY_INSERT RentalEase.dbo.tblTenant ON 
INSERT INTO RentalEase.dbo.tblTenant ([ID], [fieldname], [fieldname], ...) 
SELECT [ID], ... 
+1

你打我吧。正是我要說的。 – HLGEM 2009-09-16 21:29:25

相關問題