我在一个存储过程中加入如下语句有问题!请高手指导!小弟才加入的新人没有分给对不住各位大侠!原理如下!
有两个系统表aaa,spph,一个临时表#djmx;其中spph,aaa,#djmx字段分别为id,ph,hw,shq,sl,pqf,kxwd
临时表#djmx的值取之制单表aaa中@bs=116的值!而spph的值取之临时表#djmx的值!pqf,kxwd是系统表中后来增加的两个字段。因为工程浩大
这两个字段的取值必须是在#djmx中(pqf,kxwd).整个过程不可逆反!
现在我遇到的难题是倒手第一个IF语句!不能被执行也没有报错信息!
以下是截取一段存储过程!前提整个存储过程只有这段执行不完全!其他都没有问题?create table #djmx (
  
  id char(11) null default '',
  ph char(10) null default '',
  hw char(11) null default '',
  shq char(10) null default '',
  sl decimal(14,2) null default 0,
  pqf char(30) null default '',
  kxwd char(11) null default ''
)
if @bs='116'
  begin
--制单116(流程)
    insert into #djmx (id,ph,hw,shq,sl,pqf,kxwd)
      select 
           id,isnull(ph,''),hw,isnull(shq,''),sl,isnull(pqf,''),isnull(kxwd,'')
      from aaa (nolock) ------aaa临时表
      where gz=@gz and xz=1
  endif @bs='116' --更新spph中的pqf,kxwd的值
begin
 if (select count(*) from #djmx where pqf<>'' and kxwd<>'' )>0
begin
  update a set a.pqf=b.pqf,a.kxwd=b.kxwd  from spph a,#djmx b where a.id=b.id and a.hw=b.hw and a.ph=b.ph and a.sl=b.sl
end
    if @@error <> 0 
    begin
set @return=11
goto err_lab
    end 
end
drop table #djmx

解决方案 »

  1.   

    把if @bs='116' 判断条件合并在一起
    ...
    后来增加的两个字段?
    新增列没有default设置默认值的值为null
    pqf<>'' and kxwd<>''--如这两列是新增列是判断方法
    改为
    pqf is null and kxwd is null
      

  2.   

    顶楼上的!应该是is not null啊!你看我的先前的条件啊!isnull(pqf,''),isnull(kxwd,'');里面有为null或者为''!这个怎么解决呢?按你的方法还是不可行啊!!!!!!!!!!!!
      

  3.   

    前面的参数没有参考的值,你可以一步一步调试
    print 1 看看是否执行了
      

  4.   

    create table #djmx (
      
      id char(11) null default '',
      ph char(10) null default '',
      hw char(11) null default '',
      shq char(10) null default '',
      sl decimal(14,2) null default 0,
      pqf char(30) null default '',
      kxwd char(11) null default ''
    )
    if @bs='116'
      begin
    --制单116(流程)
        insert into #djmx (id,ph,hw,shq,sl,pqf,kxwd)
          select 
               id,isnull(ph,''),hw,isnull(shq,''),sl,isnull(pqf,''),isnull(kxwd,'')
          from aaa (nolock) ------aaa临时表
          where gz=@gz and xz=1
          
          ---加上这句测试一下
          select @@rowcount
          if (select count(*) from #djmx where pqf<>'' and kxwd<>'' )>0
    begin
      update a set a.pqf=b.pqf,a.kxwd=b.kxwd  from spph a,#djmx b where a.id=b.id and a.hw=b.hw and a.ph=b.ph and a.sl=b.sl
    end
         if @@error <> 0 
    begin
    set @return=11
    goto err_lab
    end 
      end
    drop table #djmx