How can we use a cursor in Oracle?
The usage of Oracle cursor is as follows:
- Make an official statement
DECLARE
cursor_name CURSOR FOR SELECT column1, column2, ... FROM table_name;
- Unlocked
OPEN cursor_name;
- Go and get
FETCH cursor_name INTO variable1, variable2, ...;
- Circle
- Although
LOOP
-- 处理数据的逻辑
...
-- 读取下一条记录
FETCH cursor_name INTO variable1, variable2, ...;
-- 退出循环条件
EXIT WHEN cursor_name%NOTFOUND;
END LOOP;
“or”
WHILE cursor_name%FOUND LOOP
-- 处理数据的逻辑
...
-- 读取下一条记录
FETCH cursor_name INTO variable1, variable2, ...;
END LOOP;
- Shut down
CLOSE cursor_name;
Before using a cursor, it is necessary to first define the type of cursor and the list of fields to be returned, and to open the cursor using the OPEN statement. After using it, the cursor should be closed using the CLOSE statement to release resources.