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

 

你可能感兴趣的:(SQLSERVER,存储过程,游标,案例,数据迁移)