select max(c.c_physicsname) tableName from s_removeApply a,s_removearec b,s_usertable c where a.c_usertable_id=c.id group by b.c_rec_id我用上面的sql语句查询出一个结果tableName,我想使用这个结果 作为表名查询select * from tableName可以将这两个sql写成一条吗。。

解决方案 »

  1.   

    declare @s varchar(10)
    set @s=''
    select @s=max(c.c_physicsname) tableName from s_removeApply a,s_removearec b,s_usertable c where a.c_usertable_id=c.id group by b.c_rec_id 
    exec('select * from '+@s)
      

  2.   

    declare @tab varchar(200),@sql varchar(200)
    select @tab=max(c.c_physicsname) tableName from s_removeApply a,s_removearec b,s_usertable c where a.c_usertable_id=c.id group by b.c_rec_id
    set @sql='select * from '+@tab
    exec (@sql)