--这样可否.SELECT TOP 1 *
FROM
(
   SELECT 'tb1' AS name,sid=0,* FROM tb1 WHERE id=1
   UNION ALL
   SELECT 'tb2' AS name,sid=1,* FROM tb2 WHERE id=1
   UNION ALL
   SELECT 'tb3' AS name,sid=2,* FROM tb3 WHERE id=1
   UNION ALL
   SELECT 'tb4' AS name,sid=3,* FROM tb4 WHERE id=1
   UNION ALL
   SELECT 'tb5' AS name,sid=4,* FROM tb5 WHERE id=1
   UNION ALL
   SELECT 'tb6' AS name,sid=5,* FROM tb6 WHERE id=1
   UNION ALL
   SELECT 'tb7' AS name,sid=6,* FROM tb7 WHERE id=1
   UNION ALL
   SELECT 'tb8' AS name,sid=7,* FROM tb8 WHERE id=1
   UNION ALL
   SELECT 'tb9' AS name,sid=8,* FROM tb9 WHERE id=1
   UNION ALL
   SELECT 'tb10' AS name,sid=9,* FROM tb10 WHERE id=1
) AS t
ORDER BY sid

解决方案 »

  1.   


    declare @sql varchar(100),@i int,@tbname varchar(100)
    set @i=1
    while @i<=10
    begin
        set @sql ='select * from tb'+cast(@i as varchar)+' where id =1'
        exec(@sql)
        if @@rowcount>=1
          begin
             set @tbname='tb'+cast(@i as varchar)
             print @tbname
             break
          end
        set @i=@i+1
    end
      

  2.   

    while @count<10
     begin
       @tablename = 'T' + cast(@count as varchar)
       if exist (exec ('select * from '+@tablename where id=1))
    return @tablename 
     end
      

  3.   

    封装在存储过程中依次判断create proc u_Ptest
      @ID int
    as
    if exists(select 1 from T1 where Id=@ID)
       begin 
           select  'T1'
           Return
       end if exists(select 1 from T2 where Id=@ID)
       begin 
           select  'T2'
           Return
       end ...
      

  4.   

    如果表的数据很多,用union的方法速度得不偿失
      

  5.   

    结果实现可以像2楼写的那样。除了结果,如果还要满足过程描述,那就是3楼那类做法了。静态的就是
    declare @t table(id int,f1 varchar(10),f2 datetime /*f1,f2用来模拟你的其它列*/)insert @t select id,f1,f2... from t1 where id=1
    if @@rowcount<1
    begin
    insert @t select id,f1,f2 ,, from t2 where id=1
    if @@rowcount < 1
    begin
    insert @t select id,f1,f2 ,, from t3 where id=1
    if @@rowcount < 1
    begin
    .... end end
    end
    endselect * from @t
      

  6.   


    declare @t varchar(100)
    select @t=@t + 'select top 1 * from '''+ name + '''from '
    select * from sysobjects where name in('tb1','tb2','tb3','tb4','tb5','tb6','tb7','tb8','tb9','tb10')
    set @t=@t+ 'where id=1 order by 字段'
    exec(@t)