zoukankan      html  css  js  c++  java
  • Declare and using cursor

    -- =============================================
    -- Declare and using a KEYSET cursor
    -- =============================================
    DECLARE <cursor_name, sysname, test_cursor> CURSOR
    KEYSET
    FOR <select_statement, , SELECT au_fname FROM pubs.dbo.authors>

    DECLARE @name varchar(40)

    OPEN <cursor_name, sysname, test_cursor>

    FETCH NEXT FROM <cursor_name, sysname, test_cursor> INTO @name
    WHILE (@@fetch_status <> -1)
    BEGIN
        IF (@@fetch_status <> -2)
        BEGIN
    --        PRINT 'add user defined code here'
    --        eg.
            PRINT 'updating record for ' + @name
            UPDATE pubs.dbo.authors
            SET phone = replace(phone, ' ', '-')
            WHERE CURRENT OF <cursor_name, sysname, test_cursor>
        END
        FETCH NEXT FROM <cursor_name, sysname, test_cursor> INTO @name
    END

    CLOSE <cursor_name, sysname, test_cursor>
    DEALLOCATE <cursor_name, sysname, test_cursor>
    GO

    -- =============================================
    -- Declare and using a READ_ONLY cursor
    -- =============================================
    DECLARE <cursor_name, sysname, test_cursor> CURSOR
    READ_ONLY
    FOR <select_statement, , SELECT au_fname FROM pubs.dbo.authors>

    DECLARE @name varchar(40)
    OPEN <cursor_name, sysname, test_cursor>

    FETCH NEXT FROM <cursor_name, sysname, test_cursor> INTO @name
    WHILE (@@fetch_status <> -1)
    BEGIN
        IF (@@fetch_status <> -2)
        BEGIN
    --        PRINT 'add user defined code here'
    --        eg.
            DECLARE @message varchar(100)
            SELECT @message = 'my name is: ' + @name
            PRINT @message
        END
        FETCH NEXT FROM <cursor_name, sysname, test_cursor> INTO @name
    END

    CLOSE <cursor_name, sysname, test_cursor>
    DEALLOCATE <cursor_name, sysname, test_cursor>
    GO

    -- =============================================
    -- Declare a SCROLL cursor and use various FETCH options
    -- =============================================
    DECLARE <cursor_name, sysname, test_cursor> SCROLL CURSOR FOR
    <select_statement, , SELECT * FROM pubs.dbo.authors>

    OPEN <cursor_name, sysname, test_cursor>

    -- Fetch the first row in the cursor.
    FETCH FIRST FROM <cursor_name, sysname, test_cursor>

    -- Fetch the last row in the cursor.
    FETCH LAST FROM <cursor_name, sysname, test_cursor>

    -- Fetch the row immediately prior to the current row in the cursor.
    FETCH PRIOR FROM <cursor_name, sysname, test_cursor>

    -- Fetch the row immediately after the current row in the cursor.
    FETCH NEXT FROM <cursor_name, sysname, test_cursor>

    -- Fetch the nth row in the cursor.
    FETCH ABSOLUTE <row_number, int, 8> FROM <cursor_name, sysname, test_cursor>

    -- Fetch the row that is n rows before or after the current row.
    FETCH RELATIVE <-/+ row_number, int, -2> FROM <cursor_name, sysname, test_cursor>

    CLOSE <cursor_name, sysname, test_cursor>
    DEALLOCATE <cursor_name, sysname, test_cursor>
    GO

    -- =============================================
    -- Declare and using an UPDATE cursor
    -- =============================================
    DECLARE <@variable_1, sysname, @v1> <datatype_for_variable_1, sysname, varchar(20)>,
        <@variable_2, sysname, @v2> <datatype_for_variable_2, sysname, varchar(40)>

    DECLARE    <cursor_name, sysname, test_cursor> CURSOR
    FOR SELECT <column_1, sysname, au_fname>, <column_2, sysname, au_lname> FROM <table_name, sysname, pubs.dbo.authors>
    FOR UPDATE of <column_1, sysname, au_fname>

    DECLARE @count smallint
    SELECT @count = 1

    OPEN <cursor_name, sysname, test_cursor>
    FETCH NEXT FROM <cursor_name, sysname, test_cursor> INTO <@variable_1, sysname, @v1>, <@variable_2, sysname, @v2>

    WHILE (@@fetch_status <> -1)
    BEGIN
        IF (@@fetch_status <> -2)
        BEGIN
    --        PRINT    'add user-defined code here...'
    --        eg
            PRINT 'updating record of ' + @v1 + ' ' + @v2
            UPDATE pubs.dbo.authors
            SET au_fname = @v1 + '-' + CAST(@count AS varchar(4))
            WHERE au_lname = @v2
        END
        FETCH NEXT FROM <cursor_name, sysname, test_cursor> INTO <@variable_1, sysname, @v1>, <@variable_2, sysname, @v2>
        SELECT @count = @count + 1
    END

    CLOSE <cursor_name, sysname, test_cursor>
    DEALLOCATE <cursor_name, sysname, test_cursor>
    GO

  • 相关阅读:
    Redis 发布与订阅
    Redis 数据持久化的理解
    自定义shell脚本快速搭建LNMP环境
    PHP环境配置与优化(Ubuntu16.04/PHP7)
    记录在ios系统上,自研app,灰度环境遇到的一个vue页面dom节点已渲染,但是显示部分空白的情况
    记录在苹果6p/6sp,10版本上,app内交互token等用户信息丢失的问题
    记录在苹果X手机上运行遇到的代码Dom被阻塞不更新的一个坑
    巧用flex(一)
    谷歌浏览器调试手机app内置网页
    与app交互因异步造成的坑记录
  • 原文地址:https://www.cnblogs.com/mingyongcheng/p/1995262.html
Copyright © 2011-2022 走看看