select * from mb@mb where e=1 and f=2 
 改为exec ('select * from mb'+cast(@mb as varchaar)+'where e=1 and f=2')

解决方案 »

  1.   


    declare @mb int 
    select @mb=1 
    while @mb <=30 begin 
    select * from mb where e=1 and f=2 
    set @mb=@mb+1 
    end 
      

  2.   

    declare @mb int 
    select @mb=1 
    while @mb <=30 
    begin 
    EXEC('select * from '+@mb+' where e=1 and f=2') 
    set @mb=@mb+1 
    end 
      

  3.   

    declare @mb int 
    select @mb=1 
    while @mb <=30 
    begin 
    EXEC('select * from '+@mb+' where e=1 and f=2') 
    set @mb=@mb+1 
    end 
      

  4.   

    --再次更正
    declare @mb int 
    select @mb=1 
    while @mb <=30 
    begin 
    EXEC('select * from MB['+@mb+'] where e=1 and f=2') 
    set @mb=@mb+1 
    end 
      

  5.   

    declare @mb int 
    select @mb=1 
    while @mb <=30 begin 
    exec('select * from mb'+ltrim(@mb)+' where e=1 and f=2 ')
    set @mb=@mb+1 
    end 
    要这样.
      

  6.   

    declare @mb int 
    ,@sql varchar(1000)
    select @mb=1 
    while @mb <=30 begin 
    set @sql='select * from mb'+@mb+' where e=1 and f=2 '
    exec(@sql)
    set @mb=@mb+1 
    end 
      

  7.   


    declare @mb int 
    select @mb=1 
    declare @sql  varchar(1000)
    while @mb <=30 begin 
    Set @sql='select * from mb'+@mb+' where e=1 and f=2' 
    exec (@sql)
    set @mb=@mb+1 
    end 
      

  8.   

    declare @mb int ,@s varchar(1000)
    select @mb=1 
    while @mb <=30 begin 
    set @s='select * from mb'+ltrim(@mb)+' where e=1 and f=2 '
    exec(@s)
    set @mb=@mb+1 
    end 
    错了,EXEC内不能接函数.改正.
      

  9.   

    declare @mb int 
    select @mb=1 
    while @mb <=30 begin 
    declare @sql varchar(2000) 
    set @sql=''
    set @sql=isnull(@sql,'')+'select * from '+'mb'+cast(@mb as CHAR(1))+' where e=1 and f=2 '
    print @sql
    exec(@sql)
    set @mb=@mb+1 
    end 
      

  10.   


    大家手太快
    要有    set @sql=''
    这个语句的
      

  11.   

    结果
    select * from mb1 where e=1 and f=2 
    select * from mb2 where e=1 and f=2 
    select * from mb3 where e=1 and f=2 
    select * from mb4 where e=1 and f=2 
    select * from mb5 where e=1 and f=2 
    select * from mb6 where e=1 and f=2 
    select * from mb7 where e=1 and f=2 
    select * from mb8 where e=1 and f=2 
    select * from mb9 where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
    select * from mb* where e=1 and f=2 
      

  12.   

    declare @mb int 
    select @mb=1 
    declare @sql nvarchar(50)
    while @mb <=30 
    begin
    set @sql='select * from mb'+cast(@mb as nvarchar(1))+' where e=1 and f=2'
    exec (@sql)
    set @mb=@mb+1 
    end 
      

  13.   


    declare @mb int 
    select @mb=1 
    while @mb <=30 
    begin 
    exec(N'select * from mb as '+cast(@mb as varchar)+' where e=1 and f=2') 
    set @mb=@mb+1 
    end 
      

  14.   

    declare @mb int 
    select @mb=1 
    while @mb <=30 
    begin 
    EXEC('select * from MB'+cast(@mb as varchar)+' where e=1 and f=2') 
    set @mb=@mb+1 
    end 
      

  15.   

    感谢各位大虾出手相助,提供了这么多方法,可是也许我说得不太清楚,我用着只有大虾Tony9楼的方法管用,我非常佩服,五体投地呀,这个问题我想了好多天也没办法。谢谢!也谢谢各位大虾!!