SQL 커서 의 사용법 을 자세히 설명 합 니 다.

4727 단어 SQL유표
형식:
       1.일반 커서   NEXT 만 작 동 합 니 다.
       2.스크롤 커서 는 여러 가지 동작 이 있 습 니 다.
1.일반 커서

DECLARE @username varchar(20),@UserId varchar(100)
DECLARE cursor_name CURSOR FOR --    
  SELECT TOP 10 UserId,UserName FROM UserInfo
  ORDER BY UserId DESC
OPEN cursor_name --    
FETCH NEXT FROM cursor_name INTO @UserId,@username --         
WHILE @@FETCH_STATUS = 0
  BEGIN
    PRINT '  ID:'+@UserId+'      '+'   :'+@username
    FETCH NEXT FROM cursor_name INTO @UserId,@username
  END
CLOSE cursor_name --    
DEALLOCATE cursor_name --    
결과:
사용자 ID:zhizhi            등 홍 지
사용자 ID:yuyu            위 우
사용자 ID:yujie            이옥걸
사용자 ID:yuanyuan            왕 몽 연
사용자 ID:YOU YOU            사용자 이름:lisi
사용자 ID:yiyiren            임 의
사용자 ID:yanbo            왕 연 파
사용자 ID:xuxu            사용자 이름:진가 서
사용자 ID:xiangxiang            이경 상
사용자 ID:wenwen            위 문 문
2.커서 스크롤

-- SCROLL     
SET NOCOUNT ON
DECLARE C SCROLL CURSOR FOR --SCORLL  ,         (    )
  SELECT TOP 10 UserId,UserName FROM UserInfo
  ORDER BY UserId DESC
OPEN C 
FETCH LAST FROM C  --       ,         
FETCH ABSOLUTE 4 FROM C --        4   ,             n    ,n>0    ,n<0    
FETCH RELATIVE 3 FROM C --        3   ,             n    
FETCH RELATIVE -2 FROM C --        2   ,         
FETCH PRIOR FROM C  ----        1   
FETCH FIRST FROM C  --         ,         
FETCH NEXT FROM C  --        1   

CLOSE C
DEALLOCATE C
결과(첫 번 째 결과 분석 참고 가능):

구체 적 인 FETCH 용법:

FETCH  
     [ [ NEXT | PRIOR | FIRST | LAST  
          | ABSOLUTE { n | @nvar }  
          | RELATIVE { n | @nvar }  
        ]  
        FROM  
     ]  
{ { [ GLOBAL ] cursor_name } | @cursor_variable_name }  
[ INTO @variable_name [ ,...n ] ]
Arguments
NEXT
Returns the result row immediately following the current row and increments the current row to the row returned. If FETCH NEXT is the first fetch against a cursor, it returns the first row in the result set. NEXT is the default cursor fetch option.
PRIOR
Returns the result row immediately preceding the current row, and decrements the current row to the row returned. If FETCH PRIOR is the first fetch against a cursor, no row is returned and the cursor is left positioned before the first row.
FIRST
Returns the first row in the cursor and makes it the current row.
LAST
Returns the last row in the cursor and makes it the current row.
ABSOLUTE { n| @nvar}
If n or @nvar is positive, returns the row n rows from the front of the cursor and makes the returned row the new current row. If n or @nvar is negative, returns the row n rows before the end of the cursor and makes the returned row the new current row. If n or @nvar is 0, no rows are returned. n must be an integer constant and @nvar must be smallint, tinyint, or int.
RELATIVE { n| @nvar}
If n or @nvar is positive, returns the row n rows beyond the current row and makes the returned row the new current row. If n or @nvar is negative, returns the row n rows prior to the current row and makes the returned row the new current row. If n or @nvar is 0, returns the current row. If FETCH RELATIVE is specified with n or @nvar set to negative numbers or 0 on the first fetch done against a cursor, no rows are returned. n must be an integer constant and @nvar must be smallint, tinyint, or int.
GLOBAL
Specifies that cursor_name refers to a global cursor.
cursor_name
Is the name of the open cursor from which the fetch should be made. If both a global and a local cursor exist with cursor_name as their name, cursor_name to the global cursor if GLOBAL is specified and to the local cursor if GLOBAL is not specified.
@cursor_variable_name
Is the name of a cursor variable referencing the open cursor from which the fetch should be made.
INTO @variable_name[ ,...n]
Allows data from the columns of a fetch to be placed into local variables. Each variable in the list, from left to right, is associated with the corresponding column in the cursor result set. The data type of each variable must either match or be a supported implicit conversion of the data type of the corresponding result set column. The number of variables must match the number of columns in the cursor select list.
이상 은 SQL 커서 의 용법 에 대한 상세 한 내용 입 니 다.SQL 커서 의 용법 에 관 한 자 료 는 다른 관련 글 을 주목 하 십시오!

좋은 웹페이지 즐겨찾기