undefine 员工号; declare v_sal emp.sal%type; begin select sal into v_sal from emp where empno = &&员工号; if v_sal < 3000then update emp set sal = sal + 200where empno = &&员工号; endif; dbms_output.put_line('工资为:'||v_sal); exceptionwhenno_data_foundthen dbms_output.put_line('没有该员工!'); end;
declare cursor emp_cursor isselect ename,sal from emp where deptno=10; v_ename emp.ename%type; v_sal emp.sal%type; begin open emp_cursor; loop fetch emp_cursor into v_ename,v_sal; exitwhen emp_cursor%notfound; dbms_output.put_line('姓名:'||v_ename||',工资:'||v_sal); endloop; close emp_cursor; end;
2、在游标中,使用fetch…bulk collect into语句提取所有数据
1 2 3 4 5 6 7 8 9 10 11 12 13
declare cursor emp_cursor is select ename from emp where deptno=10; type ename_table_type istableof emp.ename%type; ename_table ename_table_type; begin open emp_cursor; fetch emp_cursor bulk collect into ename_table; for i in1..ename_table.count loop dbms_output.put_line(ename_table(i)); endloop; close emp_cursor; end;
3、使用游标属性
1 2 3 4 5 6 7 8 9 10 11 12 13
declare cursor emp_cursor is select ename from emp where deptno=10; type ename_table_type istableof emp.ename%type; ename_table ename_table_type; begin ifnot emp_cursor%isopen then open emp_cursor; endif; fetch emp_cursor bulk collect into ename_table; dbms_output.put_line('提取的总行数:'||emp_cursor%rowcount); close emp_cursor; end;