请大虾们帮我看看这段命令有没有毛病,能这样用吗? exec(@s1)和 exec(@s2)是什么意思,起什么作用,我不知道,请大虾们指点一下,先谢谢。declare @hz int
select @hz=0
while @hz<=10  begin
---------------------------a-b
  declare @s1 varchar(1000)
  set @s1=
  'if Exists (
      select m.e,m.f,m.g 
        from (select a.* from hz'+ltrim(@hz)+' a,zt3 b where a.e=b.e and a.f=b.f and a.g=b.g) m,
             (select a.* from hz'+ltrim(@hz)+' a,qt1 b where a.e=b.e and a.f=b.f and a.g=b.g) q
       where m.a-m.b=q.a-q.b)   insert into hzd1 
     select n.e,n.f,n.g from zt3 z,hz'+ltrim(@hz)+' m,hz'+ltrim(@hz)+' n 
      where (z.e=m.e and z.f=m.f and z.g=m.g) and n.a-n.b!=m.a-m.b
     union all select w.e,w.f,w.g from wy w'  exec(@s1)
---------------------------a-c
  declare @s2 varchar(1000)
  set @s2=
  'if Exists (
      select m.e,m.f,m.g 
        from (select a.* from hz'+ltrim(@hz)+' a,zt3 b where a.e=b.e and a.f=b.f and a.g=b.g) m,
             (select a.* from hz'+ltrim(@hz)+' a,qt1 b where a.e=b.e and a.f=b.f and a.g=b.g) q
       where m.a-m.c=q.a-q.c)   insert into hzd2 
     select n.e,n.f,n.g from zt3 z,hz'+ltrim(@hz)+' m,hz'+ltrim(@hz)+' n 
      where (z.e=m.e and z.f=m.f and z.g=m.g) and n.a-n.c!=m.a-m.c
     union all select w.e,w.f,w.g from wy w'  exec(@s2)
----------------------
  set @hz=@hz+1 
  end 

解决方案 »

  1.   

    exec(@s1),exec(@s2)  是执行动态sql语句的, @s1,@s2 是动态语句,直接执行会报错。
      

  2.   

    exec (@sql)
    运行这个@sql字符串组成的sql语句
      

  3.   

    exec(@s1) exec(@s2)执行动态语句
    具体执行什么,因无表结构说明,难以明白.
    大概为
    表 hz0,hz1,hz2....hz10,与表zt3,qt1进行查询,如果有查询结果,
     分别对hzd1表(@s1语句中)与hzd2表(@s2语句中)插入数据while循环上年目的用于获得表hz0到hz10
    而不用逐个表分别写一段语句