最近在客戶這邊維護的時候,他們有需要把現在的資料複製到以前,應付檢查.所以我就寫了些sql來複製該資料
廢話少說,把**貼出來,大家看看
code
--刪除臨時表
drop table #tmp1
drop table #tmp2
drop table #t***
--獲取資料
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)
<
7and 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 #t*** 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 #t*** add nid nvarchar(32)
--為臨時字段賦值
update #tmp1
setnid =id
update #tmp2
setnid
=train_examine_id,
nnid =id
update #t***
setnid
=train_examine_item_id
select
*from #tmp1
select
*from #tmp2
select
*from #t***
--修改臨時表的資訊,並重新關聯
update #tmp1
setid
=replace(newid(),'-
',''),
examine_date
=dateadd(month,
6,dateadd(year,-2
,examine_date))
update #tmp2
setid
=replace(newid(),'-
',''),
train_examine_id
=#tmp1.id
from #tmp1
left join #tmp2
on #tmp1.nid
=#tmp2.nid
update #t***
settrain_examine_item_id
=#tmp2.id
from #tmp2
left join #t***
on #tmp2.nnid
=#t***.nid
--刪除臨時字段
alter table #tmp1 drop column nid
alter table #tmp2 drop column nid
alter table #tmp2 drop column nnid
alter table #t*** 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 #t***
kettle之 多表資料複製
需求 把a庫中n個表抽取至b庫中,表結構相同或只是增加統一的一些字段,過濾條件基本類似。解決方法 我們把以上內容在解決問題上拆分為二步,1,從一配製表中讀出相應的表名及過濾條件,存於一結果。2,根據第一步的結果,一條條迴圈來進行取數插數的過程。以上過程相當簡單,以下為討論的實現方式。1,如果是 or...
SQL基礎 查詢資料 多表查詢
select查詢不僅可以查詢一張表,還可以從多張表同時查詢資料 語法select from 表1 表2 同時查詢students表和classes表的 例項select from students,classes 查詢結果 一次查詢兩個表,查詢結果同樣是乙個二維表。它是students表和class...
SQL多表連線
oracle8 select a.b.from a,b where a.id b.id 相當於左聯接 select a.b.from a,b where a.id b.id 相當於右聯接 oracle9 支援以上的寫法,還增加了leftjoin right join等 select a.b.from...