首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一个SQL有关问题,求关注。

2012-12-14 
一个SQL问题,求关注。。比如有一张表为A表 此表里有个字段存放着其他表的表名数据结构如下:IDOtherTableName

一个SQL问题,求关注。。
比如有一张表为A表 此表里有个字段存放着其他表的表名

数据结构如下:
IDOtherTableNameDETAIL
1B        This is B Table
2C        This is C Table
3D        This is D Table


如果根据OtherTableName这个字段 动态的进行连接查询

例如 
SELECT * FROM A JOIN B JOIN C JOIN D

百分百结贴!

[最优解释]

declare @sql nvarchar(max)

Select @sql=insull(@sql+',','')+OtherTableName From tb 
Group by OtherTableName

Exec ('Select * From '+@sql)

[其他解释]
declare @sql nvarchar(max)
 
Select @sql=isnull(@sql+',','')+OtherTableName From tb 
Group by OtherTableName    
 
Exec ('Select * From '+@sql)

[其他解释]
1. 这设计。。。撑不住
2. join不需要条件? 那资料笔数可是 A*B*C*D 哦。
3. 实现是不难,动态sql即可。
[其他解释]
如何用动态SQL 联合起来查询呢 ?求指教!

热点排行