当前位置: 代码迷 >> Sql Server >> 怎么查询动态表
  详细解决方案

怎么查询动态表

热度:5   发布时间:2016-04-27 11:27:48.0
如何查询动态表
假设一个表comment_1,当comment_1中的数据达到上限,系统自动建立comment_2,comment_2到达上限建立comment_3……
这样如何查询呢?
补充:当comment_2没有被建立时不会查询comment_2。
求高手解答,小弟先谢过了!

------解决方案--------------------
查询的话,根据ID查,每个表的上限都设成一样的,达到多少就查哪个表,都设成1000的上限,如果是查询ID
为1001的,先判断comment_2是否存在存在就 select * from comment_2 where id=1001
------解决方案--------------------
SQL code
--下面是我的思路,仅供参考:--判断最后一个comment_N是哪个    declare @i int    declare @sql varchar(max)    set @i=10    --可以试图预估大一点    while @i>0    begin        if object_id(N'comment_'+convert(varchar(3),@i)) is null        begin            set @[email protected]        end         else        begin            set @sql=isnull(@sql+' union all ','')+' select * from '+'comment_'+convert(varchar(3),@i)        end            end    exec(@sql)
  相关解决方案