日期:2014-05-17  浏览次数:20497 次

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

数据结构如下:
ID OtherTableName DETAIL
1 B         This is B Table
2 C         This is C Table
3 D         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 联合起来查询呢 ?求指教!