📅  最后修改于: 2023-12-03 15:33:18.808000             🧑  作者: Mango
In Oracle, a cursor is a SELECT statement used to retrieve data row by row from a table or tables.
DECLARE
cursor_name CURSOR FOR
SELECT column1, column2, ... columnN FROM table_name WHERE condition;
BEGIN
-- loop to fetch cursor data
END;
In the above syntax:
cursor_name
is the identifier name of the cursorcolumn1, column2, ..., columnN
are the columns to be retrieved from the tabletable_name
is the name of the table from which data will be fetchedcondition
is the condition to be applied to filter the dataDECLARE
cursor_name CURSOR FOR
SELECT emp_name, salary FROM employees WHERE department = 'Finance';
emp_name employees.emp_name%TYPE;
salary employees.salary%TYPE;
BEGIN
OPEN cursor_name;
LOOP
FETCH cursor_name INTO emp_name, salary;
EXIT WHEN cursor_name%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee: ' || emp_name || ', Salary: ' || salary);
END LOOP;
CLOSE cursor_name;
END;
In the above example, a cursor named cursor_name
is declared and opened to retrieve employee names and salaries for those belonging to the Finance
department. A loop is used to fetch the data row by row and print it using DBMS_OUTPUT.PUT_LINE
.
Oracle cursors are useful for fetching data from tables row by row and processing it in a programmatic manner. They provide a convenient way to process large datasets and perform complex data manipulations.