不能在具有唯一索引 ‘NonClusteredIndex-20220419-093301‘ 的对象 ‘dbo.cux_machine_info‘ 中插入重复键的行。 语句已终止。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值