CURSOR 遊標使用示例

--聲明一個遊標 
DECLARE MyCursor CURSOR 
FOR SELECT TOP 5 FBookName,FBookCoding FROM TBookInfo --定義一個叫MyCursor的遊標,存放for select 後的數據 

--打開一個遊標
OPEN MyCursor--即打開這個數據集 

--循環一個遊標 
DECLARE @BookName nvarchar(2000),@BookCoding nvarchar(2000) 
FETCH NEXT FROM MyCursor INTO @BookName,@BookCoding   --移動遊標指向到第一條數據,提取第一條數據存放在變量中 
WHILE @@FETCH_STATUS =0 --若是上一次操做成功則繼續循環 
    BEGIN 
        print 'name'+@BookName 
        FETCH NEXT FROM MyCursor INTO @BookName,@BookCoding--繼續提下一行 
    END 

--關閉遊標 
CLOSE MyCursor 
--釋放資源 
DEALLOCATE MyCursor 

 

 

示例:遊標嵌套遊標的使用方法spa

 
USE [NFGC_Workflow]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:Ben.Jiang
-- Create date:2011/9/13
-- Description:添加庫存警告
-- =============================================
ALTER PROCEDURE [dbo].[Insert_AlltblStockLimit] 
    @Company NVARCHAR(50),
    @MaxEnable bit,
    @MinEnable bit,
    @Max NVARCHAR(50),
    @Min NVARCHAR(50),
    @MaxAlertAheadDays int,
    @MinAlertAheadDays int
AS
BEGIN
    DECLARE @CustomerCur CURSOR;--聲明外層遊標
    DECLARE @Id NVARCHAR(50),@ItemCode NVARCHAR(50),@customer NVARCHAR(50);
    --獲取全部客戶記錄
    SET @CustomerCur=CURSOR FOR select distinct [cust-num] from(SELECT A.[cust-num],
                                ISNULL(B.name,A.[name]) as name from dbo.Erp_custaddr a
                                 LEFT JOIN dbo.Erp_custaddr_CHS B ON A.[cust-num]=B.[cust-num] AND A.[cust-seq]=B.[cust-seq]
                                 where A.[credit-hold]='False' and A.[cust-seq]=0) as cust
                                 
    OPEN @CustomerCur --打開外層遊標
    FETCH NEXT FROM @CustomerCur INTO @customer--提取外層遊標行
    WHILE(@@FETCH_STATUS=0)
    BEGIN
    
        DECLARE @varCur CURSOR;--聲明內層遊標
        --獲取每個客戶的商品記錄
        SET @varCur = CURSOR FOR SELECT DISTINCT item FROM dbo.Erp_itemcust AS A INNER JOIN [Erp_ux-customer]
                             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
                             INNER JOIN Erp_imsAs AS C ON C.sItem=A.item AND C.sCust=A.[cust-num]  AND C.cActFlg = '0'
                             WHERE  A.item IS NOT NULL  AND LEN(A.item)>0 AND  A.[cust-num]=@customer;
         
        OPEN @varCur --打開內層遊標
        FETCH NEXT FROM @varCur INTO @ItemCode
        WHILE(@@FETCH_STATUS=0)
        BEGIN
        
        IF (exists(SELECT Id from tblStockLimit where Company=@Company and Custnum=@customer and ItemCode=@ItemCode))
            --存在該記錄更新記錄
            BEGIN
            SET @Id=(SELECT Id from tblStockLimit where Company=@Company and Custnum=@customer and ItemCode=@ItemCode)
            update tblStockLimit set MaxEnable=@MaxEnable,Max=case when @Max<>'' then @Max else null end,
            MaxAlertAheadDays=case when @MaxAlertAheadDays<>'' then @MaxAlertAheadDays else null end,
            MinEnable=@MinEnable,Min=case when @Min<>'' then @Min else null end,
            MinAlertAheadDays=case when @MinAlertAheadDays<>'' then @MinAlertAheadDays else null end where Id=@Id;
            END
        
        ELSE
            --不存在該記錄新增記錄
            BEGIN
            insert into tblStockLimit(Company,Custnum,ItemCode,MaxEnable,Max,MaxAlertAheadDays,MinEnable,Min,MinAlertAheadDays)
            values('NFGS',@customer,@ItemCode,@MaxEnable,case when @Max<>'' then @Max else null end,
            case when @MaxAlertAheadDays<>'' then @MaxAlertAheadDays else null end,@MinEnable,
            case when @Min<>'' then @Min else null end,case when @MinAlertAheadDays<>'' then @MinAlertAheadDays else null end);
            END
        
        FETCH NEXT FROM @varCur INTO @ItemCode--內層遊標向下移動一行 
        END    
        CLOSE @varCur
        DEALLOCATE @varCur
        FETCH NEXT FROM @CustomerCur INTO @customer--內層遊標結束後,外層遊標繼續向下移動一行 
    END
 
    CLOSE @CustomerCur
    DEALLOCATE @CustomerCur
END
相關文章
相關標籤/搜索