日期:2014-05-18  浏览次数:20627 次

优化存储过程
下面是对单据进行审核时执行的存储过程。以前运行起来速度还可以   ,也不会出什么错,大概一分钟就可以完成。
现在出现了如下问题:
1、存储过程执行时间长达5-15分钟;
2、经常报错或执行不了。比如退货时没有修改成负数,还是正数。
      请各位大虾帮忙解决一下!谢谢!


/*---------------------
入库出库单审核时
更新商品目录表
更新库存表
*/

CREATE   PROCEDURE   pcargosh(@orderno   varchar(50),@orderlb   varchar(50),@shrr   varchar(50))AS
set   nocount   on
if   (@orderlb= 'TH ')or(@orderlb= 'SH ')
begin  
update   pbscitems   set   quantity=-quantity,amount=-amount   where   orderno=@orderno
update   pcargo   set   quantity=-quantity,subtotal=-subtotal,   mysubtal=-mysubtal   where   orderno=@orderno
end

declare   @storename   varchar(50)
declare   @storeno1   varchar(50)
declare   @storemax   int
select   @storename=storename,@storeno1=storeno1   from   pcargo   where   orderno=@orderno

if   @orderlb= 'PD '
    begin
    update   pbscitems   set   storename=@storename   where   orderno=@orderno
    select   bookid,sum(isnull(quantity,0))   quantity,storename   into   #tmp1   from   pbscitems   where   orderno=@orderno   and   lb=@orderlb   group   by   bookid,storename
    declare   @maxpcb   int
    select   @maxpcb=isnull(max(id),0)   from   pcb
    select   identity(int,1,1)   id,bookid,quantity,storename   into   #tmp2   from   #tmp1   where   bookid   not   in(select   bookid   from   pcb   where   storename=@storename)
   
    insert   into   pcb(id,bookid,storename)select   @maxpcb+id,bookid,storename   from   #tmp2
    update   pcb   set   quantity1=quantity1+#tmp1.quantity   from   #tmp1   where   #tmp1.bookid=pcb.bookid   and   #tmp1.storename=pcb.storename
   
  return   0
    end
declare   @shr   varchar(50)
select   @shr=shr   from   pcargo   where   orderno=@orderno   and   lb=@orderlb
if   @shr   is   not   null
return   0


update   pbscitems   set   storename=@storename   where   orderno=@orderno   and   (storename   is   null   or   storename= ' ')

BEGIN   TRAN   T1
create   table   #tmptb(bookid   int   null,sl   float   null,zje   float   null,storename   varchar(50)   null)

insert   into   #tmptb(bookid,sl,zje,storename)select   pbscitems.bookid,sum(isnull(pbscitems.quantity,0)),sum(isnull(pbscitems.quantity*cargo.price,0)),pbscitems.storename   from   pbscitems,cargo   where     pbscitems.bookid=cargo.cargo_id   and   pbscitems.orderno=@orderno   group   by   pbscitems.bookid,pbscitems.storename

/*insert   into   #tmptb(bookid,sl,zje,storename)select   bookid,sum(isnull(quantity,0)),sum(isnull(amount,0)),storename   from   pbscitems   where   orderno=@orderno   group   by   bookid,storename*/

select   @storemax=isnull(max(id),0)   from   currentstore
select   identity(int,1,1)   id,bookid,storename   into   #tmptb1