declare @mb int select @mb=1 while @mb <=30 begin select * from mb where e=1 and f=2 set @mb=@mb+1 end
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
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
--再次更正 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
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 要这样.
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
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
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内不能接函数.改正.
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
大家手太快 要有 set @sql='' 这个语句的
结果 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
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
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
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
declare @mb int
select @mb=1
while @mb <=30 begin
select * from mb where e=1 and f=2
set @mb=@mb+1
end
select @mb=1
while @mb <=30
begin
EXEC('select * from '+@mb+' where e=1 and f=2')
set @mb=@mb+1
end
select @mb=1
while @mb <=30
begin
EXEC('select * from '+@mb+' where e=1 and f=2')
set @mb=@mb+1
end
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
select @mb=1
while @mb <=30 begin
exec('select * from mb'+ltrim(@mb)+' where e=1 and f=2 ')
set @mb=@mb+1
end
要这样.
,@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
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
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内不能接函数.改正.
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
大家手太快
要有 set @sql=''
这个语句的
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
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
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
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