sql server ,在把旧表数据导入到新表数据中一直出现这个错误,后面原来是设置了唯一索引,才会这样,遇到这种情况应该去检查数据,是否出现重复的键,后面删掉了才能导入
1.一个旧表数据字段导入新表
INSERT INTO [APS2022].[dbo].[新表](
[machine_sn]
,[machine_name]
,[machine_ton]
,[class_name]
,[status]
,[created_by]
,[creation_date]
,[last_update_date]
,[last_updated_by]
)
SELECT
[machine_code] [machine_sn]
,[machine_name]
,[machine_ton]
,[class_name]
,[status]
,[created_by]
,[creation_date]
,[last_update_date]
,[last_updated_by]
FROM [APS_2021].[dbo].[旧表] a
where not exists(select 1 from [APS2022].[dbo].[cux_machine_info] b
where a.machine_code=b.machine_sn and a.machine_name=b.machine_name
and a.machine_ton=b.machine_ton and a.class_name=b.class_name
and a.status=b.status and a.[created_by]=b.[created_by]
and a.[creation_date]= b.[creation_date]
and a.[last_update_date]=b.[last_update_date]
and a.[last_updated_by]=b.[last_updated_by]
)
2.2个旧表数据串联导入新表
INSERT INTO [APS2022].[dbo].[cux_machine_mold_items]
(
[machine_sn]
,[item_name]
,[mold_no]
,[priority]
,[change_times]
,[same_mold_flag]
,[start_date]
,[end_date]
,[created_by]
,[creation_date]
,[last_update_date]
,[last_updated_by]
)
SELECT
[machine_code] [machine_sn]
,[item_name]
,[mold_no]
,[priority]
,[change_times]
,[same_mold_flag]
,[start_date]
,[end_date]
,a.[created_by]
,a.[creation_date]
,a.[last_update_date]
,a.[last_updated_by]
FROM [APS_2021].[dbo].[t_machine_mold_items] a
left join [APS_2021].[dbo].[t_machine_info] b on a.machine_id=b.machine_id
where [machine_code] IS NOT NULL and a.last_update_date IS NOT NULL