动态关联表

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

原帖地址:

http://community.csdn.net/Expert/topic/3452/3452577.xml?temp=.1377375

--示例数据

create table [table](A sysname,B varchar(10))

insert [table] select 'table_1','a'

union all select 'table_2','b'

create table table_1(A int)

insert table_1 select 1

union all select 2

create table table_2(A int)

insert table_2 select 3

union all select 4

go

/*--问题说明:

table中,A字段存储着其他表的名称

A字段中,记录和各个表的A字段的最大值

即得到如下结果:

table.B C

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

a 1

b 2

--*/

--处理方法

declare @s varchar(8000)

set @s=''

select @s=@s+' when '''+A+''' then(select max(A) from ['+A+'])'

from [table] group by A

exec('select B,C=case A'+@s+' end from [table]')

go

--删除测试

drop table [table],table_1,table_2

/*--测试结果

B C

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

a 2

b 4

--*/

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航