数据迁移的问题.

brucenan999 2007-12-04 09:48:14
现有一张老表,格式如下:

Project Description Type Customer DeviceType Device
A ddd ee aaa Type1 Device1
A ddd ee aaa Type1 Device2
A ddd ee aaa Type2 Device3
B dd aaa ea Type3 Device5
C ee e aaa Type2 Device3
C ee e aaa Type2 Device4

每个项目有N个设备, 对于一个项目可能有多条记录, 冗余项目信息来表示不同的设备类型和名称.

现在分成了两张表, 主从表. 一张项目表,一张设备表,

项目表:
ProjectID Project Description Type Customer
A
B
C

设备表:
ID ProjectID DeviceType Device

请问如何用SQL把老表的数据直接导入到新表, 谢谢!!



...全文
58 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wangxuelid 2007-12-04
create @table tb(Project varchar(100),Description varchar(100),Type varchar(100),Customer varchar(100),DeviceType varchar(100),Device varchar(100))
insert into @table values('A', 'ddd', 'eE', 'aaa', 'Type1', 'Device1')
insert into @table values('A', 'ddd', 'eE', 'aaa', 'Type1', 'Device2')
insert into @table values('A', 'ddd', 'eE', 'aaa', 'Type2', 'Device3')
insert into @table values('B', 'dd' , 'aaE', 'ea' , 'Type3', 'Device5')
insert into @table values('C', 'ee' , 'eE' , 'aaa', 'Type2', 'Device3')
insert into @table values('C', 'ee' , 'eE' , 'aaa', 'Type2', 'Device4')
go
select distinct Project,Description,Type,Customer into 项目表 @table
SELECT * FROM 项目表
  • 打赏
  • 举报
回复
chenhexi007 2007-12-04
答案很全面了,呵呵,学习。
  • 打赏
  • 举报
回复
dawugui 2007-12-04
--如果项目表,设备表已经存在。
create table tb(Project varchar(10),Description varchar(10),Type varchar(10),Customer varchar(10),DeviceType varchar(10),Device varchar(10))
insert into tb values('A', 'ddd', 'ee', 'aaa', 'Type1', 'Device1')
insert into tb values('A', 'ddd', 'ee', 'aaa', 'Type1', 'Device2')
insert into tb values('A', 'ddd', 'ee', 'aaa', 'Type2', 'Device3')
insert into tb values('B', 'dd' , 'aaa', 'ea' , 'Type3', 'Device5')
insert into tb values('C', 'ee' , 'e' , 'aaa', 'Type2', 'Device3')
insert into tb values('C', 'ee' , 'e' , 'aaa', 'Type2', 'Device4')
create table 项目表(ProjectID int, Project varchar(10), Description varchar(10),Type varchar(10), Customer varchar(10))
create table 设备表(id int, project varchar(10), DeviceType varchar(10) , Device varchar(10))
go

insert into 项目表(Project , Description , Type , CusTomer , ProjectID)
SELECT * , ProjectID=(SELECT COUNT(project) FROM (select distinct Project,Description,Type,Customer from tb) t WHERE Project < a.Project) + 1 FROM (select distinct Project,Description,Type,Customer from tb) a
select * from 项目表
/*
ProjectID Project Description Type Customer
----------- ---------- ----------- ---------- ----------
1 A ddd ee aaa
2 B dd aaa ea
3 C ee e aaa

(所影响的行数为 3 行)
*/

insert into 设备表(project,DeviceType,Device,id)
select * , id = (select count(1) from (select a.project , b.DeviceType , b.Device from 项目表 a , tb b where a.project = b.project) t where (project < m.project) or (project = m.project and devicetype < m.devicetype) or (project = m.project and devicetype = m.devicetype and device < m.device)) + 1 from (select a.project , b.DeviceType , b.Device from 项目表 a , tb b where a.project = b.project) m
select * from 设备表
/*
id project DeviceType Device
----------- ---------- ---------- ----------
1 A Type1 Device1
2 A Type1 Device2
3 A Type2 Device3
4 B Type3 Device5
5 C Type2 Device3
6 C Type2 Device4

(所影响的行数为 6 行)
*/

drop table tb,项目表,设备表
  • 打赏
  • 举报
回复
dawugui 2007-12-04
create table tb(Project varchar(10),Description varchar(10),Type varchar(10),Customer varchar(10),DeviceType varchar(10),Device varchar(10))
insert into tb values('A', 'ddd', 'ee', 'aaa', 'Type1', 'Device1')
insert into tb values('A', 'ddd', 'ee', 'aaa', 'Type1', 'Device2')
insert into tb values('A', 'ddd', 'ee', 'aaa', 'Type2', 'Device3')
insert into tb values('B', 'dd' , 'aaa', 'ea' , 'Type3', 'Device5')
insert into tb values('C', 'ee' , 'e' , 'aaa', 'Type2', 'Device3')
insert into tb values('C', 'ee' , 'e' , 'aaa', 'Type2', 'Device4')
go

select ProjectID = identity(int,1,1) , * into 项目表 from (select distinct Project,Description,Type,Customer from tb) t order by project
select * from 项目表
/*
ProjectID Project Description Type Customer
----------- ---------- ----------- ---------- ----------
1 A ddd ee aaa
2 B dd aaa ea
3 C ee e aaa

(所影响的行数为 3 行)
*/

select id = identity(int,1,1) , a.project , b.DeviceType , b.Device into 设备表 from 项目表 a , tb b where a.project = b.project order by a.project , b.DeviceType , b.Device
select * from 设备表
/*
id project DeviceType Device
----------- ---------- ---------- ----------
1 A Type1 Device1
2 A Type1 Device2
3 A Type2 Device3
4 B Type3 Device5
5 C Type2 Device3
6 C Type2 Device4

(所影响的行数为 6 行)
*/

drop table tb,项目表,设备表
  • 打赏
  • 举报
回复
发帖
MS-SQL Server

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
帖子事件
创建了帖子
2007-12-04 09:48
社区公告
暂无公告