您好,登錄后才能下訂單哦!
- 什么是游標?
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;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。