您好我正在尝试使用for循环来检索oracle pl / sql中的数据但是我收到错误可以有人帮助我
SET SERVEROUTPUT ON
DECLARE
fname employees.first_name%TYPE;
empid employees.employee_id%TYPE;
CURSOR emp_cursor IS
SELECT employee_id,first_name from employees ORDER BY employee_id;
BEGIN
open emp_cursor;
FOR empid IN emp_cursor loop
DBMS_OUTPUT.PUT_LINE('employee id is ' ||empid || 'first name is '||fname);
end LOOP;
END;
我在DBMS输出行上遇到错误,例如变量的数量或类型不正确。我正在尝试从oracle示例模式中的employees表中检索员工ID和名字。
有人可以指导我吗
答案 0 :(得分:2)
在CURSORS中使用FOR..LOOP。
SET SERVEROUTPUT ON
DECLARE
/* DECLARE Cursor */
CURSOR emp_cursor IS
SELECT employee_id,first_name from employees ORDER BY employee_id;
BEGIN
FOR empid IN emp_cursor loop
/* empid already has the row details, you don't need to have any other variables */
DBMS_OUTPUT.PUT_LINE('employee id is ' ||empid.employee_id || 'first name is '||empid.first_name);
end LOOP;
END;
/
使用OPEN FETCH和CLOSE
SET SERVEROUTPUT ON
DECLARE
fname employees.first_name%TYPE;
empid employees.employee_id%TYPE;
CURSOR emp_cursor IS
SELECT employee_id,first_name from employees ORDER BY employee_id;
BEGIN
open emp_cursor;
/* LOOP until the cursor is empty */
LOOP
FETCH emp_cursor INTO empid,fname;
/* Now we fetch data from cursor, and put it into our variables */
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('employee id is ' ||empid || 'first name is '||fname);
END LOOP;
/* As we OPEN'ed the cursor manually, we have to CLOSE it without fail */
CLOSE emp_cursor;
END;
/