數據庫表間數據複製

 數據庫表間數據複製 在利用數據庫開發時,常常會將一些表之間的數據互相導入。當然可以編寫程序實現,但是,程序常常需要開發環境,不方便。最方便是利用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

---------------------------

creat到--

然後把數據庫名改成想複製到的那個庫的名稱

發佈了36 篇原創文章 · 獲贊 5 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章