SQLSERVER存储过程用游标案例

USE [TEST_DB]
GO

/****** [PRT].[Move_Data_TO_TEST_TABLE_IM]Script Date: 03/07/2012 14:36:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

/*********************************************************************************
*把数据从表 DATA_SOURCE_TABLE 移动到表 TEST_TABLE
*DataServer: 110.110.110.110
*DataBase:   TEST_DB
*Name:       [Move_Data_TO_TEST_TABLE]
*Function:   
*Input:	     overTimeHour INT
*Output:	 
*Creator:    GREATWQS 2012-02-23
*Updated:    GREATWQS 2012-02-29  UPDATE NEW REQUIREMENT
**********************************************************************************/
CREATE PROCEDURE [PRT].[Move_Data_TO_TEST_TABLE] 
    -- 超时时间(小时)
    @overTimeHour INT
AS
BEGIN
	  -- SET NOCOUNT ON added to prevent extra result sets from
	  -- interfering with SELECT statements.
	  SET NOCOUNT ON;
    
	  DECLARE
	      -- 需要插入表TEST_TABLE,数据来源声明
        @ItemNumber_Insert       CHAR(20),
        @ItemName_Insert         CHAR(50),
        @ItemColor_Insert        CHAR(10),
        @ItemSize_Insert         CHAR(5),
        @ItemWhereMadeIn_Insert  CHAR(20),
        @CreateTime_Insert       DATETIME,
        @ChangeTime_Insert       DATETIME,
        
        -- ItemNumber Record num in table
        @totalNum_SelectDB       INT,
        @changeTime_SelectDB     DATETIME
	  
	  -- Set Default Value
	  SET @CreateTime_Insert      = getdate()	
	  
    -- 1. 声明游标
	  DECLARE CURSOR_GREATWQS CURSOR
	  FORWARD_ONLY  STATIC  
        FOR 
            SELECT ItemNo,
                   ItemName,
                   ItemColor,
                   ItemSize,
                   ItemMadeIn,
                   InDate 
            FROM  PRT.DATA_SOURCE_TABLE  WITH(NOLOCK)
            WHERE InDate > dateadd(HOUR, -@overTimeHour, getdate())
            -- 在这里进行时间的限定.

    
    -- 2. 打开游标
    OPEN CURSOR_GREATWQS
	    
    -- 3. 抓取游标中的数据
    FETCH NEXT  FROM  CURSOR_GREATWQS
    INTO  @ItemNumber_Insert,
          @ItemName_Insert,
          @ItemColor_Insert,
          @ItemSize_Insert,
          @ItemWhereMadeIn_Insert,
          @ChangeTime_Insert
    
    -- 4. 对游标中的每一个记录进行处理: 循环
    WHILE (@@FETCH_STATUS = 0)
    BEGIN  
         -- 查看此ItemNumber_Insert是否已经存在表中
         SELECT TOP 1 @totalNum_SelectDB = COUNT(*) 
         FROM PRT.TEST_TABLE WITH(NOLOCK)
         WHERE ItemNumber = @ItemNumber_Insert
         
         -- 如果表不存在此@ItemNumber_Insert, 则插入
         IF @totalNum_SelectDB = 0
         BEGIN
            INSERT INTO PRT.TEST_TABLE(
                   [ItemNumber],
                   [ItemName],
                   [ItemColor],
                   [ItemSize],
                   [MadeIn],
                   [createTime],
                   [changeTime],
                   [lastChangeTime],
                   [priority],
                   [itemType],
                   [active])
             VALUES ( 
                   @ItemNumber_Insert,
                   @ItemName_Insert,
                   @ItemColor_Insert,
                   @ItemSize_Insert,
                   @ItemWhereMadeIn_Insert,
                   @CreateTime_Insert,
                   @ChangeTime_Insert,
                   NULL,
                   0,
                   0,
                   1
                  )
         END
         -- 如果此ItemNumber存在于表中,如果时间小,则更新. 
         ELSE 
         BEGIN
              -- 查看此ItemNumber_Insert的记录
              SELECT TOP 1 @changeTime_SelectDB = changeTime
              FROM PRT.TEST_TABLE WITH(NOLOCK)
              WHERE ItemNumber = @ItemNumber_Insert
              
              -- If item has exists in table, and changeTime<=newItem.changeTime 
              -- fresh the changeTime = newItem.changeTime, set active=1;
              IF @changeTime_SelectDB < @ChangeTime_Insert  
              BEGIN
                   UPDATE PRT.TEST_TABLE
                   SET changeTime = @ChangeTime_Insert, 
                       active = 1
                   WHERE ItemNumber = @ItemNumber_Insert
              END
         END
                 
         FETCH NEXT  FROM  CURSOR_GREATWQS
         INTO  @ItemNumber_Insert,
               @ItemName_Insert,
               @ItemColor_Insert,
               @ItemSize_Insert,
               @ItemWhereMadeIn_Insert,
               @ChangeTime_Insert
    END
    
    -- 5. 关闭游标
    CLOSE CURSOR_GREATWQS
    
    -- 6. 删除游标
    DEALLOCATE CURSOR_GREATWQS
    
    -- 最后一步: 删除过期任务
    UPDATE PRT.TEST_TABLE
    SET    active=0
    WHERE  changeTime < dateadd(HOUR, -@overTimeHour, getdate())

END

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值