Cursor

chunjakim·2023년 2월 12일
0

MySQL API

목록 보기
11/18
DECLARE <row variable> <type>;
DECLARE <end variable> BOOLEAN DEFAULT FALSE;
DECLARE <count variable> INT DEFAULT 0;          -- 변수 설정

DECLARE <cursor> CURSOR FOR
    <select statement>;                          -- cursor 지정

DECLARE CONTINUE HANDLER
    FOR NOT FOUND SET <end variable> = TRUE;     -- loop end 조건

OPEN <cursor>;                                   -- cursor open

<loop>: LOOP                                     -- loop 시작

    FETCH <cursor> INTO <row variable>;          -- table에서 값 받아오기

    IF <end variable> THEN                       -- table 끝까지 오면 loop 빠져나가기
        LEAVE <loop>;
    END IF;

    SET <count variable> = <count variable> + 1; -- table의 row count 세기

END LOOP <loop>;

0개의 댓글