明永成

导航

 

-- =============================================
-- 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

posted on 2011-03-25 12:21  明永成  阅读(455)  评论(0编辑  收藏  举报