最近在客户这边维护的时候,他们有需要把现在的数据复制到以前,应付检查.所以我就写了些SQL来复制该数据

废话少说,把代码贴出来,大家看看

删除临时表
drop table #tmp1
drop table #tmp2
drop table #tmp3

--获取数据
select id,company_id,examine_date,moderator_party,moderator_id 
= case when moderator_id='17adfa4692f248a180b9b4ad65835244' then '59cfe656178c45f1ab24355699f00cdb' when moderator_id='161887e22a3b46afa2033a8fa7a3585a' then '1b5a7b3472e34a1b8d350207ce6c0dac' else moderator_id  end   into #tmp1 from train_examine where year(examine_date)=2008 and month(examine_date)<7 and company_id = '35020009' order by examine_date

select 
* into #tmp2 from train_examine_item where train_examine_id in(select id from #tmp1)

select 
* into #tmp3 from train_examine_item_item where train_examine_item_id in (select id from #tmp2)

--添加临时字段
alter table #tmp1 add  NID nvarchar(
32)
alter table #tmp2 add  NID nvarchar(
32)
alter table #tmp2 add  NNID nvarchar(
32)
alter table #tmp3 add  NID nvarchar(
32)

--为临时字段赋值
update #tmp1
  
set nid = id

update #tmp2
  
set nid = train_examine_id,
      nnid 
= id

update #tmp3
  
set nid=train_examine_item_id


select 
* from #tmp1
select 
* from #tmp2
select 
* from #tmp3

--修改临时表的信息,并重新关联
update #tmp1
  
set id=replace(newid(),'-',''),
      examine_date 
=dateadd(month,6,dateadd(year,-2,examine_date))

update #tmp2 
  
set id=replace(newid(),'-',''),
      train_examine_id 
= #tmp1.id
  from #tmp1 
  left join  #tmp2 
  on #tmp1.nid 
= #tmp2.nid


update #tmp3 
  
set train_examine_item_id = #tmp2.id
  from #tmp2 
  left join  #tmp3 
  on #tmp2.nnid 
= #tmp3.nid

--删除临时字段
alter table #tmp1 drop column nid
alter table #tmp2 drop column nid
alter table #tmp2 drop column nnid
alter table #tmp3 drop column nid

--插入数据
insert into train_examine
select 
* from #tmp1
insert into train_examine_item
select 
* from #tmp2
insert into train_examine_item_item
select 
* from #tmp3



      

相关文章:

  • 2021-08-31
  • 2021-11-13
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
  • 2021-10-17
猜你喜欢
  • 2021-11-28
  • 2022-12-23
  • 2021-08-16
  • 2022-03-09
  • 2021-08-15
相关资源
相似解决方案