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

请问这个 SQL 存储过程如何优化??或更好?
CREATE   PROCEDURE   myPro   @vol       int,@num       int   ,@code   varchar(20)     AS

declare         @status   int  
set   @status   =   convert   (int,rand()*10000)

update     cost_stock      
    set     cs_end       =@vol     ,cs_status   =   @status
where   cs_end   <>   0   and   cs_type   =   'A '     and   cs_sku     =   @code

insert   into   cost_stock   (cs_sku,cs_type)
    select   cs_sku, 'B '   from   cost_stock  
          where   cs_status   =   @status   and   cs_sku     =   @code


update     cost_stock      
    set       cs_status   =   0
where     cs_type   =   'A '     and   cs_sku     =   @code

GO

谢谢~~~~


------解决方案--------------------
本身都是很简单的sql语句,没有什么优化的余地
------解决方案--------------------
update cost_stock
set cs_end =@vol ,cs_status = @status
where cs_end <> 0 and cs_type = 'A ' and cs_sku = @code

insert into cost_stock (cs_sku,cs_type)
select cs_sku, 'B ' from cost_stock
where cs_status = @status and cs_sku = @code

update cost_stock
set cs_status = 0
where cs_type = 'A ' and cs_sku = @code
--------------------------------------------

insert into cost_stock (cs_sku,cs_type)
select cs_sku, 'B ' from cost_stock
where cs_status = @status and cs_sku = @code
update cost_stock
set cs_end = (case when cs_end <> 0 then @vol else cs_end end) ,cs_status = 0
where cs_end <> 0 and cs_type = 'A ' and cs_sku = @code
-------------------------------------------
你第一句UPDATE里的cs_status = @status 不是白写吗?因为在第三句 做了不管cs_end 是不是等于0 都会让cs_status=0