資料庫表間資料複製分類

2021-04-09 10:13:53 字數 2318 閱讀 2178

資料庫表間資料複製分類:預設欄目

在利用資料庫開發時,常常會將一些表之間的資料互相匯入。當然可以編寫程式實現,但是,程式常常需要開發環境,不方便。最方便是利用sql語言直接匯入。既方便而修改也簡單。以下就是匯入的方法。

1、  表結構相同的表,且在同一資料庫(如,table1,table2)

sql :insert into table1 select  *   from table2 (完全複製)

insert into table1 select  distinct  *  from table2(不複製重複紀錄)

insert into table1 select  top 5 *  from  table2 (前五條紀錄)

2、   不在同一資料庫中(如,db1 table1,db2 table2)

sql:   insert into db1..table1 select  *   from db2..table2 (完全複製)

insert into db1..table1 select  distinct  *  from db2table2(不複製重複紀錄)

insert into tdb1..able1 select  top 5 *  from  db2table2 (前五條紀錄)

3、     表結構不同的表或複製部分紀錄(如,dn_user,dn_user2)

a.    建乙個新錶[dn_usertemp](在老表dn_user上增加一列)

create table [dn_usertemp] ( [num] [numeric](18, 0) identity (1, 1) not null)

[id] [idtype] not null ,

[name] [fntype] not null ,

[descript] [dstype] null ,

[logonnm] [idtype] not null ,

[password] [idtype] null ,

[gender] [char] (1) null ,

[quited] [booltype] not null,

[offduty] [booltype] not null ,

[stopped] [booltype] not null, 

[osbind] [booltype] not null, 

[domain] [idtype] null ,

[email] [fntype] null ,

[unitid] [idtype] null ,

[branchid] [idtype] null ,

[dutyid] [idtype] null ,

[levelid] [idtype] null ,

[classid] [idtype] null ,

[typeid] [idtype] null ,

[ip] [varchar] (15) collate chinese_prc_ci_as null ,

[expiredt] [datetime] null ,

[sort] [int] not null ,

[allowdel] [booltype] not null,

[unitchief] [booltype] not null, 

[branchchief] [booltype] not null ,

[unitdeputy] [booltype] not null ,

[branchdeputy] [booltype] not null ,

[num] [numeric](18, 0) identity (1, 1) not null 

) on [primary]

b. 將dn_uer2的資料拷入dn_usertemp

sql:insert into dn_usertemp select * from dn_user2 

c.將dn_usertemp 拷入dn_user

sql:

declare  @i int

declare  @j int

declare  @name fntype

set @i=1

select @j=count(*) from dn_usertemp

while @i<@j 1

begin

select @name=name from dn_usertemp where num=@i

print @name

insert into dn_user (name) values (@name) where num=@i

select @i=@i 1

end

資料庫表資料複製

同乙個資料庫中不同表之間的資料複製,比如我想將乙個資料庫demo中的table1表的資料複製到demo資料庫中表table2中 我們可以這樣寫 insert into table1 name,password money select name,password money from table2 ...

資料庫表複製

select into from 和 insert into select都是用來複製表,兩者的主要區別為 select into from 要求目標表不存在,因為在插入時會自動建立。insert into select from 要求目標表存在 備份表資料 create table emp as ...

HGDB兩表間資料複製

目錄 文件用途 詳細資訊 文件用途 本文件提供select into和insert into select兩種表複製語句的使用方法及示例。詳細資訊 在hgdb中的select into和insert into select兩種表複製語句都可以用來複製表與表之間的資料。1.insert into fr...