竖表写入横表

streamIF 2011-06-21 12:24:50
表1数据

select * from 表1
结果:

字段1
------------
100
200
300
...

能不能通过一条语句写入一个临时表2

表2结构
u_1 int,u_2 int,u_3 int.....(按表1的行数动态增加 :u_? int)
...全文
82 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
patrickjiang 2011-06-21
  • 打赏
  • 举报
回复
Mark!
streamIF 2011-06-21
  • 打赏
  • 举报
回复

我想qianjin036a的方法一定是可以的,非常感谢,可能是ms2000不支持 pivot的原因

我冒昧的问一下如果,我的表2是存在的实表,
结构如下
u_id,u_field1,u_field2....u_field40

我想将表1的数据从u_field1开始写入 (可以保证表1的行数不会大于40)
请教一下语句怎么写,不用游标?

表1数据

select * from 表1
结果:

字段1
------------
100
200
300
...
streamIF 2011-06-21
  • 打赏
  • 举报
回复
我用的是MS2000
streamIF 2011-06-21
  • 打赏
  • 举报
回复
...
(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 12 行)

服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '(' 附近有语法错误。
服务器: 消息 208,级别 16,状态 1,行 1
对象名 '##2' 无效。
服务器: 消息 3701,级别 11,状态 5,行 1
无法 除去 表 '##2',因为它在系统目录中不存在。
-晴天 2011-06-21
  • 打赏
  • 举报
回复
全局临时表真是个好东东哪~~~
-晴天 2011-06-21
  • 打赏
  • 举报
回复
要写入临时表..
修正:
create table tb(col int)
insert into tb select 2
insert into tb select 2
insert into tb select 4
insert into tb select 7
insert into tb select 34
insert into tb select 23
insert into tb select 44
insert into tb select 12
insert into tb select 3
insert into tb select 6
insert into tb select 8
insert into tb select 19
go
select id=identity(int,1,1),col into ##1 from tb
declare @s nvarchar(4000)
select @s=isnull(@s+',','')+'['+ ltrim(id) +']' from(
select distinct [id] from ##1
)t
exec('select '+@s+' into ##2 from ##1 pivot (max([col]) for id in('+@s+'))b')
select * from ##2
/*
1 2 3 4 5 6 7 8 9 10 11 12
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2 2 4 7 34 23 44 12 3 6 8 19

(1 行受影响)
*/
go
drop table tb,##1,##2
-晴天 2011-06-21
  • 打赏
  • 举报
回复
create table tb(col int)
insert into tb select 2
insert into tb select 2
insert into tb select 4
insert into tb select 7
insert into tb select 34
insert into tb select 23
insert into tb select 44
insert into tb select 12
insert into tb select 3
insert into tb select 6
insert into tb select 8
insert into tb select 19
go
select id=identity(int,1,1),col into ##1 from tb
declare @s nvarchar(4000)
select @s=isnull(@s+',','')+'['+ ltrim(id) +']' from(
select distinct [id] from ##1
)t
exec('select '+@s+'from ##1 pivot (max([col]) for id in('+@s+'))b')

/*
1 2 3 4 5 6 7 8 9 10 11 12
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2 2 4 7 34 23 44 12 3 6 8 19

(1 行受影响)
*/
go
drop table tb,##1
FlySQL 2011-06-21
  • 打赏
  • 举报
回复
这个需要用动态SQL,不过有个问题,如果两个表的个数不一致就比较难搞
AcHerat 元老 2011-06-21
  • 打赏
  • 举报
回复
这。。。。。。
cd731107 2011-06-21
  • 打赏
  • 举报
回复
create table tb(col int)
insert into tb select 100
insert into tb select 200
insert into tb select 300
insert into tb select 400
insert into tb select 500

select id=identity(int,1,1),col into # from tb

declare @sql varchar(8000)
set @sql = ''
select @sql = @sql + ',(select '+ltrim(col)+' [u_'+ltrim(id)+']) a'+ltrim(id)
from #
set @sql =' select * from '+stuff(@sql,1,1,'')
print (@sql)
exec(@sql)


--select * from (select 100 [u_1]) a1,(select 200 [u_2]) a2,(select 300 [u_3]) a3,(select 400 [u_4]) a4,(select 500 [u_5]) a5

drop table tb,#
-晴天 2011-06-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 streamif 的回复:]
我想qianjin036a的方法一定是可以的,非常感谢,可能是ms2000不支持 pivot的原因

我冒昧的问一下如果,我的表2是存在的实表,
结构如下
u_id,u_field1,u_field2....u_field40

我想将表1的数据从u_field1开始写入 (可以保证表1的行数不会大于40)
请教一下语句怎么写,不用游标?

表1数据

select * fr……
[/Quote]
这一句:
select * from ##2
改成:
insert into 表2(id,1,2,3,4,5,...,原表最大行数) select @youruserid,* from ##2

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧