当源和目标都有数据和标识列时,SQL 导入数据

IT *_*her 2 sql-server ssms ssis sql-server-2008-r2 import

我正在使用导入和导出数据选项将数据从一个数据库导入到另一个数据库。两个数据库具有相似的表结构。所有表都有主键,即标识元素(自动加 1)。源表和目标表都有行。

现在,当我导入数据时,我收到类似的错误 Failure inserting into the read-only column

所以我启用了enable identity insert复选框并尝试导入数据。在这种情况下,我得到如下所示的错误

Copying to [dbo].[Misc_Data] (Error)
Messages
Error 0xc0202009: Data Flow Task 1: SSIS Error Code DTS_E_OLEDBERROR.  An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 10.0"  Hresult: 0x80004005  Description: "The statement has been terminated.".
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 10.0"  Hresult: 0x80004005  Description: "Violation of PRIMARY KEY constraint 'PK_Misc_Data'. Cannot insert duplicate key in object 'dbo.Misc_Data'. The duplicate key value is (1).".
(SQL Server Import and Export Wizard)

Error 0xc0209029: Data Flow Task 1: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR.  The "input "Destination Input" (50)" failed because error code 0xC020907B occurred, and the error row disposition on "input "Destination Input" (50)" specifies failure on error. An error occurred on the specified object of the specified component.  There may be error messages posted before this with more information about the failure.
(SQL Server Import and Export Wizard)

Error 0xc0047022: Data Flow Task 1: SSIS Error Code DTS_E_PROCESSINPUTFAILED.  The ProcessInput method on component "Destination - Misc_Data" (37) failed with error code 0xC0209029 while processing input "Destination Input" (50). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.  There may be error messages posted before this with more information about the failure.
(SQL Server Import and Export Wizard)
Run Code Online (Sandbox Code Playgroud)

我认为当我在 SQL 上启用身份插入时,尝试插入值与身份列的源表中相同的行并给出错误。

那么在这种情况下如何导入数据。当源和目标中都有数据时,标识列被设置为主键。

编辑 我用来导入表的查询除了一列(身份列)如下所示

SELECT [Name]
  ,[Status]
  ,[Last_Checked]
  ,[RecievedDate]
  ,[RecptNo]
  ,[Date2]
  ,[processedDate]
  ,[ind_num]
  ,[AcNo]
  ,[returnType]
  ,[sign]
  ,[oldStatus]
  ,[Acno_old]
  ,[Remarks]
  ,[FileName]
  ,[Status_checked]
  ,[Date_Time]
  ,[By]
Run Code Online (Sandbox Code Playgroud)

FROM [dbname].[dbo].[mytable]

在这种情况下,我会收到类似的错误

- Pre-execute (Error)
Messages
Error 0xc0202004: Data Flow Task 1: The number of columns is incorrect.
Run Code Online (Sandbox Code Playgroud)

(SQL Server 导入和导出向导)

Error 0xc0202025: Data Flow Task 1: Cannot create an OLE DB accessor. Verify that the column metadata is valid.
 (SQL Server Import and Export Wizard)

Error 0xc004701a: Data Flow Task 1: component "Destination - Query" (73) failed the pre-execute phase and returned error code 0xC0202025.
(SQL Server Import and Export Wizard)
Run Code Online (Sandbox Code Playgroud)

两个表具有相同的结构和相同的数据类型。我仍然收到错误。

Zan*_*ane 13

不要只使用整个表。而是选择编写查询的选项。

基本说明

然后只需编写一个不使用该标识列的选择语句。 如此基础

或者,当您映射列时,如果您单击“删除目标表中的行”,它将在加载数据之前截断目标表。如果存在从源系统中删除数据的机会并且它应该保留在目标中,那么此步骤将不适合您。

在此处输入图片说明

你的包裹看起来像这样

在此处输入图片说明

  • 徒手“截断”是炸弹的挖掘,哟! (2认同)