sql游标以及异常处理


    DECLARE @emsgoutid UNIQUEIDENTIFIER ;
    DECLARE @sendtime VARCHAR(20) ;
    DECLARE @temptime DATETIME ;
    
    DECLARE sms_cursor CURSOR FOR
    SELECT emsgoutid FROM dbo.emsg_out

    -- 打开游标
    OPEN sms_cursor
    FETCH NEXT FROM sms_cursor
        INTO @emsgoutid
        
    WHILE @@FETCH_STATUS = 0
        BEGIN
            --try
            BEGIN TRY
            
                SELECT  @sendtime = sendtime
                FROM    dbo.emsg_out
                WHERE   emsgoutid = @emsgoutid ;
                SET @temptime = CAST(@sendtime AS DATETIME)
                
                --SELECT CAST('Nov  4 2011 11:03AM' AS DATETIME)
        
            END TRY
            BEGIN CATCH
            
                PRINT @emsgoutid ;
                DECLARE @msg NVARCHAR(2000)
                SET @msg = ERROR_MESSAGE()
                RAISERROR(@msg,16,1)
                
            END CATCH
    
            FETCH NEXT FROM sms_cursor
            INTO @emsgoutid
            
        END --end while
    
    --关闭游标
    CLOSE sms_cursor
    DEALLOCATE sms_cursor

   
posted @ 2011-11-30 12:03  深潭  阅读(392)  评论(0编辑  收藏  举报