一个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)