十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
- 什么是游标?
我们提供的服务有:成都网站设计、成都做网站、外贸网站建设、微信公众号开发、网站优化、网站认证、达州ssl等。为上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的达州网站制作公司
DECLARE
--定义游标
CURSOR emp_cursor IS SELECT empno, ename, job FROM emp;
v_empno emp.empno%TYPE;
v_ename emp.ename%TYPE;
v_job emp.job%TYPE;
BEGIN
--打开游标,执行查询
OPEN emp_cursor;
--提取数据
LOOP
FETCH emp_cursor INTO v_empno, v_ename, v_job;
DBMS_OUTPUT.PUT_LINE('员工号:' || v_empno || ',姓名' || v_ename || ',职位' || v_job);
--什么时候退出循环?%FOUND,%NOTFOUND
EXIT WHEN emp_cursor%NOTFOUND; --EXIT WHEN NOT emp_cursor%FOUND;
END LOOP;
--关闭游标
CLOSE emp_cursor;
END;
1、%FOUND:该属性用于检测游标结果集是否存在数据,如果存在数据,返回TRUE。
2、%NOTFOUND:该属性用于检测结果集是否存在数据,如果不存在数据,返回TRUE。
3、%ISOPEN:该属性用于检测游标是否已经打开,如果已经打开返回TURE。
4、%ROWCOUNT:该属性用于返回已经提取的实际行数。(如EXIT WHEN emp_cursor%ROWCOUNT=5;)
示例:按职工的职称涨工资,总裁涨1000元,经理涨500元,其他员工涨300元。
DECLARE
--定义游标
CURSOR emp01_cursor IS SELECT empno, job FROM emp01;
v_empno emp01.empno%TYPE;
v_job emp01.job%TYPE;
BEGIN
--打开游标,执行查询
OPEN emp01_cursor;
--提取数据
LOOP
FETCH emp01_cursor INTO v_empno, v_job;
IF v_job ='PRESIDENT' THEN
UPDATE emp01 SET sal = sal + 1000 WHERE empno = v_empno;
ELSIF v_job = 'MANAGER' THEN
UPDATE emp01 SET sal = sal + 500 WHERE empno = v_empno;
ELSE
UPDATE emp01 SET sal = sal + 300 WHERE empno = v_empno;
END IF;
--什么时候退出循环?%FOUND,%NOTFOUND
EXIT WHEN NOT emp01_cursor%FOUND;
END LOOP;
COMMIT;
CLOSE emp01_cursor;
--关闭游标
END;
FOR record_name IN cursor_name(或者可以使用子查询) LOOP statement;
END LOOP;
DECLARE
CURSOR emp_cursor IS SELECT empno, ename, job FROM emp;
BEGIN
FOR emp_record IN emp_cursor LOOP
DBMS_OUTPUT.put_line('员工号:' || emp_record.empno || ', 姓名' || ',职位' || emp_record.job);
END LOOP;
END;
BEGIN
FOR emp_record IN (SELECT empno, ename, job FROM emp) LOOP
DBMS_OUTPUT.put_line('员工号:' || emp_record.empno || ', 姓名' || ',职位' || emp_record.job);
END LOOP;
END;
DECLARE
--定义游标
CURSOR emp01_cursor IS SELECT empno, job FROM emp01;
BEGIN
FOR emp01_record IN emp01_cursor LOOP
DBMS_OUTPUT.put_line(emp01_record.empno || '----' || emp01_record.job);
IF emp01_record.job = 'PRECIDENT' THEN
UPDATE emp01 SET sal = sal + 1000 WHERE empno = emp01_record.empno;
ELSIF emp01_record.job = 'MANAGER' THEN
UPDATE emp01 SET sal = sal + 500 WHERE empno = emp01_record.empno;
ELSE
UPDATE emp01 SET sal = sal + 300 WHERE empno = emp01_record.empno;
END IF;
END LOOP;
COMMIT;
END;
CURSOR cursor_name (parameter_name datatype) IS select_statement;
OPEN cursor_name (parameter_value);
DECLARE
CURSOR emp_cursor(dno NUMBER) IS SELECT empno, ename, job FROM emp WHERE deptno = dno;
BEGIN
FOR emp_record IN emp_cursor(10) LOOP
DBMS_OUTPUT.put_line('员工号:' || emp_record.empno || ',姓名:' || emp_record.ename || ',职位:' || emp_record.job);
END LOOP;
END;
SQL%FOUND
SQL%NOTFOUND
SQL%ISOPEN
SQL%ROWCOUNT
BEGIN
UPDATE emp01 SET sal = 100 + sal WHERE empno = &n1;
IF SQL%FOUND THEN
dbms_output.put_line('成功修改员工的工资');
ELSE
dbms_output.put_line('修改员工工资失败');
ROLLBACK;
END IF;
END;
CURSOR cursor_name IS select_statement FOR UPDATE [OF column_reference] [NOWAIT];
UPDATE table_name SET column = ... WHERE CURRENT OF cursor_name;
DELETE FROM table_name WHERE CURRENT OF cursor_name;
DECLARE
--定义游标
CURSOR emp01_cursor IS SELECT empno, job FROM emp01 FOR UPDATE;
BEGIN
FOR emp01_record IN emp01_cursor LOOP
DBMS_OUTPUT.put_line(emp01_record.empno || '----' || emp01_record.job);
IF emp01_record.job = 'PRECIDENT' THEN
UPDATE emp01 SET sal = sal + 1000 WHERE CURRENT OF emp01_cursor;
ELSIF emp01_record.job = 'MANAGER' THEN
UPDATE emp01 SET sal = sal + 500 WHERE CURRENT OF emp01_cursor;
ELSE
UPDATE emp01 SET sal = sal + 300 WHERE CURRENT OF emp01_cursor;
END IF;
END LOOP;
COMMIT;
END;
DECLARE
CURSOR emp01_cursor IS
SELECT d.dname dname, e.ename ename
FROM emp01 e join dept d on e.deptno = d.deptno
WHERE e.deptno = &deptno
FOR UPDATE OF e.deptno;
BEGIN
FOR emp01_record IN emp01_cursor LOOP
dbms_output.put_line('部门名称:' || emp01_record.dname || ',员工名:' || emp01_record.ename);
DELETE FROM emp01 WHERE CURRENT OF emp01_cursor;
END LOOP;
COMMIT;
END;