+ -
当前位置:首页 → 问答吧 → 非固定数量列显示

非固定数量列显示

时间:2011-11-25

来源:互联网

表1

列名编号 名称 
 1 a
 2 b
 3 c
 4 d
 5 e

表2
编号 1 2 3 4 5
001 r r d s s
002 d d f f v

通过这两个表想要的效果:
编号 a b c d e
001 r r d s s
002 d d f f v
......

其中表1的列名编号和表2的1,2,3,4,5是名称相同。相当于把表2的1,2,3,4,5猎头 替换成 表1的名称


作者: wangquan77731   发布时间: 2011-11-25

SQL code
--> --> (Roy)生成測試數據
 
if not object_id('表1') is null
    drop table 表1
Go
Create table 表1([列名编号] int,[名称] nvarchar(1))
Insert 表1
select 1,N'a' union all
select 2,N'b' union all
select 3,N'c' union all
select 4,N'd' union all
select 5,N'e'
Go


--> --> (Roy)生成測試數據
 
if not object_id('表2') is null
    drop table 表2
Go
Create table 表2([编号] nvarchar(3),[1] nvarchar(1),[2] nvarchar(1),[3] nvarchar(1),[4] nvarchar(1),[5] nvarchar(1))
Insert 表2
select N'001',N'r',N'r',N'd',N's',N's' union all
select N'002',N'd',N'd',N'f',N'f',N'v'
Go
declare @s nvarchar(1000)
set @s='select [编号] '
Select @s=@s+',' +quotename([列名编号])+' as '+quotename([名称])from 表1  
print @s
exec(@s+' from 表2')
/*
编号    a    b    c    d    e
001    r    r    d    s    s
002    d    d    f    f    v
*/

作者: roy_88   发布时间: 2011-11-25

谢谢,请问这些代码能ACCESS中 编译通过吗?

作者: wangquan77731   发布时间: 2011-11-25

引用 1 楼 roy_88 的回复:
SQL code
--> --> (Roy)生成測試數據

if not object_id('表1') is null
drop table 表1
Go
Create table 表1([列名编号] int,[名称] nvarchar(1))
Insert 表1
select 1,N'a' union all
select 2,N'b' union all
sele……

作者: fredrickhu   发布时间: 2011-11-25

引用 2 楼 wangquan77731 的回复:
谢谢,请问这些代码能ACCESS中 编译通过吗?

昏迷 不能。

作者: fredrickhu   发布时间: 2011-11-25

ACCESS 里可以通过 VBA 来实现的.

作者: qianjin036a   发布时间: 2011-11-25

不能。
引用 2 楼 wangquan77731 的回复:
谢谢,请问这些代码能ACCESS中 编译通过吗?

作者: szstephenzhou   发布时间: 2011-11-25

论坛怎么了,好多时候打不开贴子

作者: pengxuan   发布时间: 2011-11-25

ACCESS? 跑到这发帖干嘛

作者: HJ_daxian   发布时间: 2011-11-25