请大虾们帮我看看下面的语句怎样修改,谢谢!declare @e int,@f int,@g int select @qe=10,@2f=20,@g=30declare @mb int
select @mb=0
while @mb<=10begindeclare @QE intset @QE=(select a.e from mb'+ltrim(@mb)+' a where a.e=@e and a.f=@f and a.g=@g)set @mb=@mb+1 end 错误提示服务器: 消息 170,级别 15,状态 1,行 22
第 13 行: '+ltrim(@mb)+' 附近有语法错误。

解决方案 »

  1.   

    declare @e int,@f int,@g int select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin 
    exec(
    'declare @QE int select @qe=a.e from mb'+@mb+' a where a.e=@e and a.f=@f and a.g=@g
    select @qe' ) set @mb=@mb+1 end 
      

  2.   

    修改declare @e int,@f int,@g int select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin 
    exec(
    'declare @QE int select @qe=a.e from mb'+@mb+' a where a.e'=@e +'and a.f='@f +'and a.g'=@g
    ' select @qe' ) set @mb=@mb+1 end 
      

  3.   

    set @QE=(select a.e from mb +ltrim(@mb) a where a.e=@e and a.f=@f and a.g=@g) 
      

  4.   

    declare @e int,@f int,@g int ,@s varchar(8000)select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin 
    set @s=
    'declare @QE int select @qe=a.e from mb'+ltrim(@mb)+' a where a.e'=@e +'and a.f='@f +'and a.g'=@g
    ' select @qe' set @mb=@mb+1 
    exec(@s)
    end 
      

  5.   

    你的表名是动态的,要用sp_executesql。
    看一下这个,自己改改。
    declare @aa int,@TableName nvarchar(20),@sqlstring nvarchar(4000)
    set @TableName = 'aaa'
    set @sqlstring=N'select @aa = count(F1) from  '+@TableName
    exec sp_executesql @sqlstring,N'@aa int output',@aa output
    print @aa
      

  6.   

    你print(@s)出来看看 你的语法都错误了
      

  7.   

    declare @e int,@f int,@g int ,@s varchar(8000)select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin
      
    set @s=
    'declare @QE int
    select @qe=a.e from mb'+ltrim(@mb)+' a where a.e='+ltrim(@e) +' and a.f='+ltrim(@f) +' and a.g='+ltrim(@g)+' select @qe' 
    set @mb=@mb+1 
    exec(@s)
    end 
      

  8.   

    declare @e int,@f int,@g int ,@s varchar(8000)select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin
      
    set @s=
    'declare @QE int
    select @qe=a.e from mb'+ltrim(@mb)+' a where a.e='+ltrim(@e) +' and a.f='+ltrim(@f) +' and a.g='+ltrim(@g)+' select @qe' 
    set @mb=@mb+1 
    exec(@s)
    end 
    下面是print(@s)的语句:
    declare @QE int
    select @qe=a.e from mb0 a where a.e=10 and a.f=20 and a.g=30 select @qe
    declare @QE int
    select @qe=a.e from mb1 a where a.e=10 and a.f=20 and a.g=30 select @qe
    declare @QE int
    select @qe=a.e from mb2 a where a.e=10 and a.f=20 and a.g=30 select @qe
    declare @QE int
    select @qe=a.e from mb3 a where a.e=10 and a.f=20 and a.g=30 select @qe
    declare @QE int
    select @qe=a.e from mb4 a where a.e=10 and a.f=20 and a.g=30 select @qe
    declare @QE int
    select @qe=a.e from mb5 a where a.e=10 and a.f=20 and a.g=30 select @qe
    declare @QE int
    select @qe=a.e from mb6 a where a.e=10 and a.f=20 and a.g=30 select @qe
    declare @QE int
    select @qe=a.e from mb7 a where a.e=10 and a.f=20 and a.g=30 select @qe
    declare @QE int
    select @qe=a.e from mb8 a where a.e=10 and a.f=20 and a.g=30 select @qe
    declare @QE int
    select @qe=a.e from mb9 a where a.e=10 and a.f=20 and a.g=30 select @qe
    declare @QE int
    select @qe=a.e from mb10 a where a.e=10 and a.f=20 and a.g=30 select @qe
      

  9.   


    declare @e int,@f int,@g int ,@s varchar(8000)select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin
      
    set @s=
    'declare @QE int
    select @qe=a.e from mb'+ltrim(@mb)+' a where a.e='+ltrim(@e) +' and a.f='+ltrim(@f) +' and a.g='+ltrim(@g)+' select @qe' 
    set @mb=@mb+1 
    exec(@s)
    end 
      

  10.   

    9楼的有点问题 
    改下:
    [code=SQL]declare @e int,@f int,@g int ,@s varchar(8000)select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin
      
    set @s=
    'declare @QE int
    set @qe=a.e from mb'+ltrim(@mb)+' a where a.e='+ltrim(@e) +' and a.f='+ltrim(@f) +' and a.g='+ltrim(@g)+' 
    select @qe' 
    set @mb=@mb+1 
    exec(@s)
    end [/code]
      

  11.   


    declare @e int,@f int,@g int ,@s varchar(8000)select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin
      
    set @s=
    'declare @QE int
    select @qe=a.e from mb'+ltrim(@mb)+' a where a.e='+ltrim(@e) +' and a.f='+ltrim(@f) +' and a.g='+ltrim(@g) 
    set @mb=@mb+1 
    print(@s)
    end declare @QE int
    select @qe=a.e from mb0 a where a.e=10 and a.f=20 and a.g=30
    declare @QE int
    select @qe=a.e from mb1 a where a.e=10 and a.f=20 and a.g=30
    declare @QE int
    select @qe=a.e from mb2 a where a.e=10 and a.f=20 and a.g=30
    declare @QE int
    select @qe=a.e from mb3 a where a.e=10 and a.f=20 and a.g=30
    declare @QE int
    select @qe=a.e from mb4 a where a.e=10 and a.f=20 and a.g=30
    declare @QE int
    select @qe=a.e from mb5 a where a.e=10 and a.f=20 and a.g=30
    declare @QE int
    select @qe=a.e from mb6 a where a.e=10 and a.f=20 and a.g=30
    declare @QE int
    select @qe=a.e from mb7 a where a.e=10 and a.f=20 and a.g=30
    declare @QE int
    select @qe=a.e from mb8 a where a.e=10 and a.f=20 and a.g=30
    declare @QE int
    select @qe=a.e from mb9 a where a.e=10 and a.f=20 and a.g=30
    declare @QE int
    select @qe=a.e from mb10 a where a.e=10 and a.f=20 and a.g=30应该要这样
      

  12.   

    declare @e int,@f int,@g int ,@s varchar(8000)select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin
      
    set @s=
    'declare @QE'+ltrim(@mb)+'  int
    select @qe=a.e from mb'+ltrim(@mb)+' a where a.e='+ltrim(@e) +' and a.f='+ltrim(@f) +' and a.g='+ltrim(@g) 
    set @mb=@mb+1 
    print(@s)
    end declare @QE0  int
    select @qe=a.e from mb0 a where a.e=10 and a.f=20 and a.g=30
    declare @QE1  int
    select @qe=a.e from mb1 a where a.e=10 and a.f=20 and a.g=30
    declare @QE2  int
    select @qe=a.e from mb2 a where a.e=10 and a.f=20 and a.g=30
    declare @QE3  int
    select @qe=a.e from mb3 a where a.e=10 and a.f=20 and a.g=30
    declare @QE4  int
    select @qe=a.e from mb4 a where a.e=10 and a.f=20 and a.g=30
    declare @QE5  int
    select @qe=a.e from mb5 a where a.e=10 and a.f=20 and a.g=30
    declare @QE6  int
    select @qe=a.e from mb6 a where a.e=10 and a.f=20 and a.g=30
    declare @QE7  int
    select @qe=a.e from mb7 a where a.e=10 and a.f=20 and a.g=30
    declare @QE8  int
    select @qe=a.e from mb8 a where a.e=10 and a.f=20 and a.g=30
    declare @QE9  int
    select @qe=a.e from mb9 a where a.e=10 and a.f=20 and a.g=30
    declare @QE10  int
    select @qe=a.e from mb10 a where a.e=10 and a.f=20 and a.g=30
      

  13.   

    declare @e int,@f int,@g int 
    select @e=10,@f=20,@g=30 
    declare @mb int 
    select @mb=0 
    declare @QE int
    declare @mbstr varchar(2) 
    while @mb <=10 
    begin 
    set @mbstr = convert(varchar(2),@mb)
    exec('select '+@QE+'=a.e from mb'+@mbstr+' a where a.e='+@e+' and a.f='+@f+' and a.g='+@g) 
    set @mb=@mb+1 
    end 
      

  14.   

    declare @e int,@f int,@g int ,@s varchar(8000)select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin
      
    set @s=
    'declare @QE'+ltrim(@mb)+'  int
    select @qe=a.e from mb'+ltrim(@mb)+' a where a.e='+ltrim(@e) +' and a.f='+ltrim(@f) +' and a.g='+ltrim(@g)+ char(10)+char(13)+ 'select  @QE'+ltrim(@mb)
    set @mb=@mb+1 
    print(@s)
    end declare @QE0  int
    select @qe=a.e from mb0 a where a.e=10 and a.f=20 and a.g=30
    select  @QE0
    declare @QE1  int
    select @qe=a.e from mb1 a where a.e=10 and a.f=20 and a.g=30
    select  @QE1
    declare @QE2  int
    select @qe=a.e from mb2 a where a.e=10 and a.f=20 and a.g=30
    select  @QE2
    declare @QE3  int
    select @qe=a.e from mb3 a where a.e=10 and a.f=20 and a.g=30
    select  @QE3
    declare @QE4  int
    select @qe=a.e from mb4 a where a.e=10 and a.f=20 and a.g=30
    select  @QE4
    declare @QE5  int
    select @qe=a.e from mb5 a where a.e=10 and a.f=20 and a.g=30
    select  @QE5
    declare @QE6  int
    select @qe=a.e from mb6 a where a.e=10 and a.f=20 and a.g=30
    select  @QE6
    declare @QE7  int
    select @qe=a.e from mb7 a where a.e=10 and a.f=20 and a.g=30
    select  @QE7
    declare @QE8  int
    select @qe=a.e from mb8 a where a.e=10 and a.f=20 and a.g=30
    select  @QE8
    declare @QE9  int
    select @qe=a.e from mb9 a where a.e=10 and a.f=20 and a.g=30
    select  @QE9
    declare @QE10  int
    select @qe=a.e from mb10 a where a.e=10 and a.f=20 and a.g=30
    select  @QE10这样应该符合楼主的意思了
      

  15.   

    declare @e int,@f int,@g int ,@s varchar(8000)select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin
      
    set @s=
    'declare @QE'+ltrim(@mb)+'  int
    select @QE'+ltrim(@mb)+'=a.e from mb'+ltrim(@mb)+' a where a.e='+ltrim(@e) +' and a.f='+ltrim(@f) +' and a.g='+ltrim(@g)+ char(10)+char(13)+ 'select  @QE'+ltrim(@mb)
    set @mb=@mb+1 
    print(@s)
    end declare @QE0  int
    select @QE0=a.e from mb0 a where a.e=10 and a.f=20 and a.g=30
    select  @QE0
    declare @QE1  int
    select @QE1=a.e from mb1 a where a.e=10 and a.f=20 and a.g=30
    select  @QE1
    declare @QE2  int
    select @QE2=a.e from mb2 a where a.e=10 and a.f=20 and a.g=30
    select  @QE2
    declare @QE3  int
    select @QE3=a.e from mb3 a where a.e=10 and a.f=20 and a.g=30
    select  @QE3
    declare @QE4  int
    select @QE4=a.e from mb4 a where a.e=10 and a.f=20 and a.g=30
    select  @QE4
    declare @QE5  int
    select @QE5=a.e from mb5 a where a.e=10 and a.f=20 and a.g=30
    select  @QE5
    declare @QE6  int
    select @QE6=a.e from mb6 a where a.e=10 and a.f=20 and a.g=30
    select  @QE6
    declare @QE7  int
    select @QE7=a.e from mb7 a where a.e=10 and a.f=20 and a.g=30
    select  @QE7
    declare @QE8  int
    select @QE8=a.e from mb8 a where a.e=10 and a.f=20 and a.g=30
    select  @QE8
    declare @QE9  int
    select @QE9=a.e from mb9 a where a.e=10 and a.f=20 and a.g=30
    select  @QE9
    declare @QE10  int
    select @QE10=a.e from mb10 a where a.e=10 and a.f=20 and a.g=30
    select  @QE10
    老是考虑不周到
      

  16.   

    谢谢各位大虾出的热心相助。不知从@s 中声明的变量@QE在同一个循环中能不能应用于别的varchar变量中。
      

  17.   


    declare @e int,@f int,@g int 
    select @e=10,@f=20,@g=30 
    declare @mb int 
    select @mb=0 while @mb <=10 
    begin 
    declare @QE varchar(1000) 
    set @QE='declare @QE'+cast(@mb as varchar)+'  int
    select a.e from mb'+cast(@mb as varchar)+' a where a.e='+ cast(@e as varchar)+ 'and a.f='+cast(@f as varchar) +'and a.g='+cast(@g as varchar) 
    print(@mb)
    print(@QE)
    set @mb=@mb+1 
    end 结果为
    0
    declare @QE0  int 
    select @QE0=a.e from mb0 a where a.e=10 and a.f=20 and a.g=30
    1
    declare @QE1  int
    select @QE1=a.e from mb1 a where a.e=10 and a.f=20 and a.g=30
    2
    declare @QE2  int
    select @QE2=a.e from mb2 a where a.e=10 and a.f=20 and a.g=30
    3
    declare @QE3  int
    select @QE3=a.e from mb3 a where a.e=10 and a.f=20 and a.g=30
    4
    declare @QE4  int
    select @QE4=a.e from mb4 a where a.e=10 and a.f=20 and a.g=30
    5
    declare @QE5  int
    select @QE5=a.e from mb5 a where a.e=10 and a.f=20 and a.g=30
    6
    declare @QE6  int
    select @QE6=a.e from mb6 a where a.e=10 and a.f=20 and a.g=30
    7
    declare @QE7  int
    select @QE7=a.e from mb7 a where a.e=10 and a.f=20 and a.g=30
    8
    declare @QE8  int
    select @QE8=a.e from mb8 a where a.e=10 and a.f=20 and a.g=30
    9
    declare @QE9  int
    select @QE9=a.e from mb9 a where a.e=10 and a.f=20 and a.g=30
    10
    declare @QE10  int
    select @QE10=a.e from mb10 a where a.e=10 and a.f=20 and a.g=30这应该是正确的。。楼上sql哥很犀利。。楼主想什么都知道。。不过你那应该不会打印出select @QE(1,2,3,4,5...)吧。。
      

  18.   

    我晕。。这么长。。没看到后面的。。不好意思啊,sql哥。。
      

  19.   

    declare @e int,@f int,@g int ,@s varchar(8000)select @e=10,@f=20,@g=30 declare @mb int 
    select @mb=0 
    while @mb <=10 begin 
    set @s=
    'declare @QE int select @qe=a.e from mb'+ltrim(@mb)+' a where a.e'=@e +'and a.f='@f +'and a.g'=@g
    ' select @qe' set @mb=@mb+1 
    exec(@s)
    end