SQL Server 存储过程之嵌套游标

SQL库存警告设置
本文介绍了一个SQL过程,用于为特定公司、客户和商品配置库存上限和下限警告。通过内外层游标遍历所有有效客户及其对应商品,检查并更新或插入库存限制信息。
  1. USE [NFGC_Workflow]  
  2. GO  
  3. SET ANSI_NULLS ON  
  4. GO  
  5. SET QUOTED_IDENTIFIER ON  
  6. GO  
  7. -- =============================================  
  8. -- Author:Ben.Jiang  
  9. -- Create date:2011/9/13  
  10. -- Description:添加库存警告  
  11. -- =============================================  
  12. ALTER PROCEDURE [dbo].[Insert_AlltblStockLimit]   
  13.     @Company NVARCHAR(50),  
  14.     @MaxEnable bit,  
  15.     @MinEnable bit,  
  16.     @Max NVARCHAR(50),  
  17.     @Min NVARCHAR(50),  
  18.     @MaxAlertAheadDays int,  
  19.     @MinAlertAheadDays int  
  20. AS  
  21. BEGIN  
  22.     DECLARE @CustomerCur CURSOR;--声明外层游标  
  23.     DECLARE @Id NVARCHAR(50),@ItemCode NVARCHAR(50),@customer NVARCHAR(50);  
  24.     --获取所有客户记录  
  25.     SET @CustomerCur=CURSOR FOR select distinct [cust-num] from(SELECT A.[cust-num],  
  26.                                 ISNULL(B.name,A.[name]) as name from dbo.Erp_custaddr a  
  27.                                  LEFT JOIN dbo.Erp_custaddr_CHS B ON A.[cust-num]=B.[cust-num] AND A.[cust-seq]=B.[cust-seq]  
  28.                                  where A.[credit-hold]='False' and A.[cust-seq]=0) as cust  
  29.                                    
  30.     OPEN @CustomerCur --打开外层游标  
  31.     FETCH NEXT FROM @CustomerCur INTO @customer--提取外层游标行  
  32.     WHILE(@@FETCH_STATUS=0)  
  33.     BEGIN  
  34.       
  35.         DECLARE @varCur CURSOR;--声明内层游标  
  36.         --获取每一个客户的商品记录  
  37.         SET @varCur = CURSOR FOR SELECT DISTINCT item FROM dbo.Erp_itemcust AS A INNER JOIN [Erp_ux-customer]  
  38.                              AS B ON  A.[cust-num]=B.[cust-num]  AND B.[cust-num] IS NOT NULL   AND LEN(A.[cust-num])>0 AND B.[cust-seq]=0  
  39.                              INNER JOIN Erp_imsAs AS C ON C.sItem=A.item AND C.sCust=A.[cust-num]  AND C.cActFlg = '0'  
  40.                              WHERE  A.item IS NOT NULL  AND LEN(A.item)>0 AND  A.[cust-num]=@customer;  
  41.            
  42.         OPEN @varCur --打开内层游标  
  43.         FETCH NEXT FROM @varCur INTO @ItemCode  
  44.         WHILE(@@FETCH_STATUS=0)  
  45.         BEGIN  
  46.           
  47.         IF (exists(SELECT Id from tblStockLimit where Company=@Company and Custnum=@customer and ItemCode=@ItemCode))  
  48.             --存在该记录更新记录  
  49.             BEGIN  
  50.             SET @Id=(SELECT Id from tblStockLimit where Company=@Company and Custnum=@customer and ItemCode=@ItemCode)  
  51.             update tblStockLimit set MaxEnable=@MaxEnable,Max=case when @Max<>'' then @Max else null end,  
  52.             MaxAlertAheadDays=case when @MaxAlertAheadDays<>'' then @MaxAlertAheadDays else null end,  
  53.             MinEnable=@MinEnable,Min=case when @Min<>'' then @Min else null end,  
  54.             MinAlertAheadDays=case when @MinAlertAheadDays<>'' then @MinAlertAheadDays else null end where Id=@Id;  
  55.             END  
  56.           
  57.         ELSE  
  58.             --不存在该记录新增记录  
  59.             BEGIN  
  60.             insert into tblStockLimit(Company,Custnum,ItemCode,MaxEnable,Max,MaxAlertAheadDays,MinEnable,Min,MinAlertAheadDays)  
  61.             values('NFGS',@customer,@ItemCode,@MaxEnable,case when @Max<>'' then @Max else null end,  
  62.             case when @MaxAlertAheadDays<>'' then @MaxAlertAheadDays else null end,@MinEnable,  
  63.             case when @Min<>'' then @Min else null end,case when @MinAlertAheadDays<>'' then @MinAlertAheadDays else null end);  
  64.             END  
  65.           
  66.         FETCH NEXT FROM @varCur INTO @ItemCode--内层游标向下移动一行   
  67.         END   
  68.         CLOSE @varCur  
  69.         DEALLOCATE @varCur  
  70.         FETCH NEXT FROM @CustomerCur INTO @customer--内层游标结束后,外层游标继续向下移动一行   
  71.     END  
  72.   
  73.     CLOSE @CustomerCur  
  74.     DEALLOCATE @CustomerCur  
  75. END
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值