表a1(no,name,class),a2(no,name,class).....a100.....以此类推,表结构都一样,但是有大量表,每天按日期增加一张。求查询所有表中,name=‘123'的记录。最好能在过了很久以后,尽可能少的修改语句后,就能重复使用。

解决方案 »

  1.   

    写一个视图把所有表UNION ALL起来,然后写一个作业每天更新视图把新加的表加进视图,查询视图就可以了
      

  2.   

    把SQL SERVER当成Excel用了,服
      

  3.   

    select *
    from a1 where name='123'
    union all
    select *
    from a2 where name='123'
    ....这设计...
      

  4.   

    如果表少还可以 如果是大量的表的话这样的设计是有问题的  写个sql语句要把人累死
      

  5.   

    select * into #a from a1 where 1=2
    declare @tablename varchar(20)
    declare @i int
    select @i = 1
    while(@i <= 按日期增加循环)
    begin 
        select @tablename = '' --按日期定义表名
        declare @sql varchar(1000)
        
        select @sql = 'insert into #a select * from ' + @tablename + ' where name = ''123'''
        exec(@sql)
        
        select @i = @i + 1
    endselect * from #a