有一表
ID D1 D2 D3 D4
1 AB B C AB
2 B AB B BC
现将记录进行拆分,如第一条记录拆分为4条记录
1 A B C B
2 A B C A
3 B B C B
4 B B C A
即每个字段区一个字母进行组合。
第二条记录同样如此拆分。
并将拆分后的记录存到另外的表中!
ID D1 D2 D3 D4
1 AB B C AB
2 B AB B BC
现将记录进行拆分,如第一条记录拆分为4条记录
1 A B C B
2 A B C A
3 B B C B
4 B B C A
即每个字段区一个字母进行组合。
第二条记录同样如此拆分。
并将拆分后的记录存到另外的表中!
解决方案 »
- 关于一个sql语句的写法
- **********************请问数据库镜像的问题***************
- 安装sql报错 microsoft sql server 2000 standard edition 服务器组件在操作系统上不支持。只有客户端组件才可用于安装
- 两个表作比较sql查询语句
- 库位分析的问题?
- 急啊急啊,SQL Server 2000连接问题,大家来帮忙?
- 整数转换为固定长度字符串的问题
- JDBC调用含有复制表语句的存储过程无返回值问题
- 菜鸟问题:如何判断一个表是否存在
- 如何得到最新插入/修改的记录 (在线等待)
- oracle序列和sqlserver自增有什么区别
- 汉字转拼音
ID D1 D2 D3 D4
1 AB B C AB
2 B AB B BC
现将记录进行拆分,如第一条记录拆分为4条记录
ID D1 D2 D3 D4
1 A B C B
2 A B C A
3 B B C B
4 B B C A
即每个字段取分别一个字母进行组合成一条记录。
第二条记录同样如此拆分。
ID D1 D2 D3 D4
5 B B B C
6 B B B B
7 B A B C
8 B A B B
并将拆分后的记录存到另外的表中!
结果为
ID D1 D2 D3 D4
1 A B C B
2 A B C A
3 B B C B
4 B B C A
5 B B B C
6 B B B B
7 B A B C
8 B A B B
if object_id('tempdb.dbo.#') is not null drop table #
create table #(ID int, D1 varchar(8), D2 varchar(8), D3 varchar(8), D4 varchar(8))
insert into #
select 1, 'AB', 'B', 'C', 'AB' union all
select 2, 'B', 'AB', 'B', 'BC'if object_id('tempdb.dbo.#n') is not null drop table #n
select top 10 n = identity(int,1,1) into #n from syscolumns;
with t1 as
(
select id, d1=substring(d1,n,1) from #, #n where n<=len(d1)
),
t2 as
(
select id, d2=substring(d2,n,1) from #, #n where n<=len(d2)
),
t3 as
(
select id, d3=substring(d3,n,1) from #, #n where n<=len(d3)
),
t4 as
(
select id, d4=substring(d4,n,1) from #, #n where n<=len(d4)
)
select id=row_number()over(order by t1.id), d1,d2,d3,d4 from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t3.id and t3.id=t4.id
/*
id d1 d2 d3 d4
-------- ---- ---- ---- ----
1 A B C A
2 A B C B
3 B B C A
4 B B C B
5 B A B B
6 B B B B
7 B A B C
8 B B B C
*/
用一下脑,久了不用会生锈的inset your_table select id=row_number()over(order by t1.id), d1,d2,d3,d4 from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t3.id and t3.id=t4.idselect id=row_number()over(order by t1.id), d1,d2,d3,d4 into your_table from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t3.id and t3.id=t4.id