请大虾们帮我看看下面的命令是哪出了问题,红色数字若为两位数或三位数则命令完成,不提示错误。
declare
@q2a int,@q2b int,@q2c int,@q2d int,@q1a int,@q1b int,@q1c int,@q1d int,@zta int,@ztb int,@ztc int,@ztd int,@sq2a nvarchar(1000),@sq2b nvarchar(1000),@sq2c nvarchar(1000),@sq2d nvarchar(1000),
@sq1a nvarchar(1000),@sq1b nvarchar(1000),@sq1c nvarchar(1000),@sq1d nvarchar(1000),
@szta nvarchar(1000),@sztb nvarchar(1000),@sztc nvarchar(1000),@sztd nvarchar(1000),@s1 nvarchar(1000),@mb intselect @mb=0
while @mb<=10
beginset @sq2a='select @q2a=a.a from mb'+ltrim(@mb)+' a where a.e=1 and a.f=2 and a.g=3 '
exec sp_executesql @sq2a,N'@q2a int output',@q2a output
set @sq2b='select @q2b=a.b from mb'+ltrim(@mb)+' a where a.e=1 and a.f=2 and a.g=3 '
exec sp_executesql @sq2b,N'@q2b int output',@q2b output
set @sq2c='select @q2c=a.c from mb'+ltrim(@mb)+' a where a.e=1 and a.f=2 and a.g=3 '
exec sp_executesql @sq2c,N'@q2c int output',@q2c output
set @sq2d='select @q2d=a.d from mb'+ltrim(@mb)+' a where a.e=1 and a.f=2 and a.g=3 '
exec sp_executesql @sq2d,N'@q2d int output',@q2d outputset @sq1a='select @q1a=a.a from mb'+ltrim(@mb)+' a where a.e=4 and a.f=5 and a.g=6 '
exec sp_executesql @sq1a,N'@q1a int output',@q1a output
set @sq1b='select @q1b=a.b from mb'+ltrim(@mb)+' a where a.e=4 and a.f=5 and a.g=6 '
exec sp_executesql @sq1b,N'@q1b int output',@q1b output
set @sq1c='select @q1c=a.c from mb'+ltrim(@mb)+' a where a.e=4 and a.f=5 and a.g=6 '
exec sp_executesql @sq1c,N'@q1c int output',@q1c output
set @sq1d='select @q1d=a.d from mb'+ltrim(@mb)+' a where a.e=4 and a.f=5 and a.g=6 '
exec sp_executesql @sq1d,N'@q1d int output',@q1d outputset @szta='select @zta=a.a from mb'+ltrim(@mb)+' a where a.e=7 and a.f=8 and a.g=9 '
exec sp_executesql @szta,N'@zta int output',@zta output
set @sztb='select @ztb=a.b from mb'+ltrim(@mb)+' a where a.e=7 and a.f=8 and a.g=9 '
exec sp_executesql @sztb,N'@ztb int output',@ztb output
set @sztc='select @ztc=a.c from mb'+ltrim(@mb)+' a where a.e=7 and a.f=8 and a.g=9 '
exec sp_executesql @sztc,N'@ztc int output',@ztc output
set @sztd='select @ztd=a.d from mb'+ltrim(@mb)+' a where a.e=7 and a.f=8 and a.g=9 '
exec sp_executesql @sztd,N'@ztd int output',@ztd outputset @s1=
  'if Exists ('+ltrim(@zta) +'-'+ltrim(@ztb) +'='+ltrim(@q1a) +'-'+ltrim(@q1b) +' or '+ltrim(@zta) +'-'+ltrim(@ztb) +'-'+ltrim(@ztc) +'-'+ltrim(@ztd) +'='+ltrim(@q1a) +'-'+ltrim(@q1b) +'-'+ltrim(@q1c) +'-'+ltrim(@q1d) +')
      insert into mbd1 select n.e,n.f,n.g from mb'+ltrim(@mb)+' n where ('+ltrim(@zta) +'-'+ltrim(@ztb) +'!=n.a-n.b and '+ltrim(@zta) +'-'+ltrim(@ztb) +'-'+ltrim(@ztc) +'-'+ltrim(@ztd) +'!=n.a-n.b-n.c-n.d)
      union all select 15,15,15 where Exists (select 1 from mb'+ltrim(@mb)+' n where ('+ltrim(@zta) +'-'+ltrim(@ztb) +'!=n.a-n.b and '+ltrim(@zta) +'-'+ltrim(@ztb) +'-'+ltrim(@ztc) +'-'+ltrim(@ztd) +'!=n.a-n.b-n.c-n.d))'
exec(@s1)set @mb=@mb+1 
end 
提示错误:服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '1' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '2' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '3' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '4' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '3' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '3' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '3' 附近有语法错误。
.
.
.
.

解决方案 »

  1.   

    DECLARE @MB VARCHAR(120)
    SET @MB='FDAS'
    DECLARE @sq2a NVARCHAR(120)SET   @sq2a=N'select @q2a=a.a from mb'+ltrim(@mb)+' a where a.e=1 and a.f=2 and a.g=3 '
    PRINT @sq2a 
    /*
    select @q2a=a.a from mbFDAS a where a.e=1 and a.f=2 and a.g=3 ???这个没问题呀
      

  2.   

    a.e数据类型是?
    rtrim(ltrim(a.e))=1
      

  3.   

    set @sq2a='select @q2a=a.a from mb'+ltrim(@mb)+' a where a.e='+ltrim(1)+ ' and a.f='+ltrim(2)+' and a.g='+ltrim(3)  
    那你每行都换成这样试试 
    不过估计没用 
      

  4.   

    你要是不明白这个错误,可以把语句print出来看下
    print @sq2a看下这个值是什么,也就明白是哪里错了
      

  5.   

    set @sq2a='select @q2a=a.a from mb'+ltrim(@mb)+' a where a.e=1 and a.f=2 and a.g=3 '看一下AND后面的空格
      

  6.   

    估计还是数据类型的错误 改成varchar试下
      

  7.   


    显示:select @q2a=a.a from mb10 a where a.e=1 and a.f=2 and a.g=3 
      

  8.   

    select @q2a=a.a from mb10 a where a.e=1 and a.f=2 and a.g=3 有结果?
      

  9.   

    找到原因了,却掉蛇足Exists。感谢大虾们热心相助。