求救!怎样根据某列的值关联不同的表

ronaldoking9 2008-03-18 03:11:39
有一表某一字段存储的值是表名(有很多个)
tb结构如下
序号 时间 地点 主题 表名
1 . . . table1
2 . . . table2
3 . . . table3
4 . . . table4
5 . . . table5
6 . . . table6
. . . . .
. . . . .
. . . . .
. . . . .
其中table1,table2,table3等等的结构相同
现在根据时间获取tb里的内容后,再关联到table1,table2等等中取数据,该如何处理?
想得到的结果集如下:
序号 时间 地点 主题 字段1 字段2
1 . . . table1.字段1 table1.字段2
2 . . . table2.字段1 table2.字段2
3 . . . table3.字段1 table3.字段2
...全文
107 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluefad 2009-09-24
  • 打赏
  • 举报
回复
学习关联表知识,不得已回复老帖子
xiaoliaoyun 2008-03-18
  • 打赏
  • 举报
回复
我是用我自己的表做测试,上面的有点小问题,select top 1 字段1,字段2 这里

declare @xuhao int
declare @count int
declare @table varchar(50)
declare @date datetime
declare @sql varchar(8000)
set @sql = ''
set @date = '2008-3-1'
select identity(int,1,1) as orders,序号,表名 into #temp from tb where 时间 = @date order by 序号 desc
select @count = count(1) from #temp
while @count > 0
begin
select @xuhao = 序号,@table = 表名 from #temp where orders = @count

set @sql = @sql+' union all select 序号,时间,地点,主题,字段1,字段2
from tb,(select top 1 字段1,字段2 from '+@table+')a where 序号 = ' + cast(@xuhao as varchar(10))

set @count = @count - 1
end
drop table #temp
set @sql = substring(@sql,12,8000)
print @sql
exec( @sql)
xiaoliaoyun 2008-03-18
  • 打赏
  • 举报
回复

declare @xuhao int
declare @count int
declare @table varchar(50)
declare @date datetime
declare @sql varchar(8000)
set @sql = ''
set @date = '2008-3-1'
select identity(int,1,1) as orders,序号,表名 into #temp from tb where 时间 = @date order by 序号 desc
select @count = count(1) from #temp
while @count > 0
begin
select @xuhao = 序号,@table = 表名 from #temp where orders = @count

set @sql = @sql+' union all select 序号,时间,地点,主题,字段1,字段2
from tb,(select top 1 区域 from '+@table+')a where 序号 = ' + cast(@xuhao as varchar(10))

set @count = @count - 1
end
drop table #temp
set @sql = substring(@sql,12,8000)
print @sql
exec( @sql)
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
没人顶吗?
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
耐心等候指教
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
我还是不知道怎样写这样的动态sql,这是关键,请指点一下吧,谢谢!
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
就拿我上面的例子帮我写个例子好吗?table1等表里也不要取其他数据,就取count(*)作为汇总好了
dawugui 2008-03-18
  • 打赏
  • 举报
回复
那就是自己组合了.
至于怎样组合,得看你自己的表结构来定了.
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
序号 时间 地点 主题 字段1 字段2
1 . . . table1.汇总1 table1.汇总2
2 . . . table2.汇总1 table2.汇总2
3 . . . table3.汇总1 table3.汇总2

就是得到这样的纪录集,谢谢你的帮忙,呵呵
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
就是得到我想要结果的那种阿,先是根据时间段获取tb里的内容,然后类似左连接一样动态得到table1里的内容,就是vtb里的一行对应table1里的一行(因为取得是table1里的汇总计算,所以只有一行),以此类推tb里的第二行对应table2里的一行
dawugui 2008-03-18
  • 打赏
  • 举报
回复
你要怎样的动态?
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
需要完整的例子,你上面写的刚才我自己写过了
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
你这个不是动态的阿
dawugui 2008-03-18
  • 打赏
  • 举报
回复
declare @tbname as varchar(20)
select @tbname = 表名 from tb where 序号 = 1
exec('select * from ' + @tbname)
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
2楼的,怎么不说具体的解决方法啊,再等着你帮忙呢,我也知道是动态sql,但对于这个问题我没法解决啊,给出答案吧
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
忘记说了,都是取一行的纪录,即
1 . . . table1.字段1 table1.字段2
中table1.字段1 也是只取一行值
2 . . . table2.字段1 table2.字段2
中table2.字段1 也是只取一行值
以此类推
ronaldoking9 2008-03-18
  • 打赏
  • 举报
回复
具体怎么操作?楼上的帮帮忙
dawugui 2008-03-18
  • 打赏
  • 举报
回复
貌似只能用动态SQL来完成了.
不知道楼主是否满意.

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧