一个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 联合起来查询呢 ?求指教!