SQLServer游标的使用

CREATE PROCEDURE AotuCancelOrders 
AS
 
BEGIN TRANSACTION tran_test
 
SAVE TRANSACTION tran_test
 
declare @Pid int,
  
@prodid int,
  
@Prodcount int

 
declare Aotu_cursor  cursor for 
 
select Pid,Pprodid FROM ProdOrder where Porderid in(select Ordersid from  Orders where (DATEDIFF(minute,ObespeakDate,getdate())>14400and (Ostate<>3 and Ostate<>4))
 
 
open Aotu_cursor
 
fetch next from Aotu_cursor into @Pid,@prodid
 
WHILE @@FETCH_STATUS = 0
 
begin
   
set @Prodcount=(select pcount from ProdOrder where Pid=@Pid)

   
update ShopItems set SNumber=SNumber+@Prodcount where SIid=@prodid

   
fetch next from Aotu_cursor into @Pid,@prodid
 
end

 
UPDATE Orders set Ostate=4 where (DATEDIFF(minute,ObespeakDate,getdate())>14400and (Ostate<>3 and Ostate<>4)
 
COMMIT TRANSACTION tran_test
 
        
IF ( @@ERROR <> 0 )
        
BEGIN
           
ROLLBACK TRANSACTION tran_test
        
END
    
        
IF ( @@TRANCOUNT > 0 )
        
BEGIN
           
ROLLBACK TRANSACTION tran_test
        
END

 
close Aotu_cursor--关闭
  DEALLOCATE Aotu_cursor--释放

GO

posted @ 2008-08-07 09:16  supers  阅读(216)  评论(0)    收藏  举报