--查詢所有員工的員工號、姓名和職位的信息。DECLARE --定義游標 CURSOR emp_cursor IS SELECT empno,ename,job FROM emp; v_empno emp.empno%TYPE; v_ename emp.ename%TYPE; v_job emp.j ...
--查詢所有員工的員工號、姓名和職位的信息。
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;
END LOOP;
--關閉游標
CLOSE emp_cursor;
END;
--查詢所有員工的員工號、姓名和職位的信息。
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;
--檢測游標是否打開
IF emp_cursor%ISOPEN THEN
DBMS_OUTPUT.PUT_LINE('游標已經打開');
ELSE
DBMS_OUTPUT.PUT_LINE('游標沒有打開');
END IF;
END;
--游標FOR迴圈
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.ename||',職位:'||emp_record.job);
END LOOP;
END;
--游標FOR迴圈中引用子查詢
BEGIN
FOR emp_record IN (SELECT empno,ename,job FROM emp) LOOP
DBMS_OUTPUT.PUT_LINE('員工號:'||emp_record.empno||',姓名:'||emp_record.ename||',職位:'||emp_record.job);
END LOOP;
END;
--參數游標
DECLARE
CURSOR emp_cursor(dno NUMBER) IS SELECT empno,ename,job FROM emp WHERE deptno = dno;
BEGIN
FOR emp_record IN emp_cursor(&no) LOOP
DBMS_OUTPUT.PUT_LINE('員工號:'||emp_record.empno||',姓名:'||emp_record.ename||',職位:'||emp_record.job);
END LOOP;
END;
--根據用戶輸入的員工號,更新指定員工的工資,比如工資漲100
--隱式游標
BEGIN
UPDATE empnew SET sal = sal + 100 WHERE empno = &no;
IF SQL%FOUND THEN
DBMS_OUTPUT.put_line('成功修改員工的工資');
COMMIT;
ELSE
DBMS_OUTPUT.put_line('修改員工工資失敗!');
ROLLBACK;
END IF;
END;
SELECT * FROM empnew;
--按職工的職稱漲工資,總裁漲1000元,經理漲500元,其他員工漲300元。
--1:用顯示游標的常規方式實現業務需求
DECLARE
--定義游標
CURSOR empnew_cursor IS SELECT empno,job FROM empnew;
v_empno empnew.empno%TYPE;
v_job empnew.job%TYPE;
BEGIN
--打開游標
OPEN empnew_cursor;
--提取數據
LOOP
FETCH empnew_cursor INTO v_empno,v_job;
IF v_job='PRESIDENT' THEN
UPDATE empnew SET sal = sal + 1000 WHERE empno = v_empno;
ELSIF v_job='MANAGER' THEN
UPDATE empnew SET sal = sal + 500 WHERE empno = v_empno;
ELSE
UPDATE empnew SET sal = sal + 300 WHERE empno = v_empno;
END IF;
EXIT WHEN empnew_cursor%NOTFOUND;
END LOOP;
COMMIT;
--關閉游標
CLOSE empnew_cursor;
END;
--2:用游標FOR迴圈的方式實現業務需求
DECLARE
--定義游標
CURSOR empnew_cursor IS SELECT empno,job FROM empnew;
BEGIN
FOR empnew_record IN empnew_cursor LOOP
DBMS_OUTPUT.put_line(empnew_record.empno||'----'||empnew_record.job);
IF empnew_record.job = 'PRESIDENT' THEN
UPDATE empnew SET sal = sal + 1000 WHERE empno = empnew_record.empno;
ELSIF empnew_record.job = 'MANAGER' THEN
UPDATE empnew SET sal = sal + 500 WHERE empno = empnew_record.empno;
ELSE
UPDATE empnew SET sal = sal + 300 WHERE empno = empnew_record.empno;
END IF;
END LOOP;
--COMMIT;
END;
select * from empnew for update;
--3:使用游標添加或刪除數據時,定義游標時利用FOR UPDATE 子句可以將游標提取出來的數據進行行級鎖定
DECLARE
--定義游標
CURSOR empnew_cursor IS SELECT empno,job FROM empnew FOR UPDATE;
BEGIN
FOR empnew_record IN empnew_cursor LOOP
DBMS_OUTPUT.put_line(empnew_record.empno||'----'||empnew_record.job);
IF empnew_record.job = 'PRESIDENT' THEN
UPDATE empnew SET sal = sal + 1000 WHERE CURRENT OF empnew_cursor;
ELSIF empnew_record.job = 'MANAGER' THEN
UPDATE empnew SET sal = sal + 500 WHERE CURRENT OF empnew_cursor;
ELSE
UPDATE empnew SET sal = sal + 300 WHERE CURRENT OF empnew_cursor;
END IF;
END LOOP;
COMMIT;
END;
SELECT * FROM empnew;
--FOR UPDATE NOWAIT 不等待鎖,如發現所操作的數據行已經鎖定,將不會等待,立即返回
DECLARE
--定義游標
CURSOR empnew_cursor IS SELECT empno,job FROM empnew FOR UPDATE NOWAIT;
BEGIN
FOR empnew_record IN empnew_cursor LOOP
DBMS_OUTPUT.put_line(empnew_record.empno||'----'||empnew_record.job);
IF empnew_record.job = 'PRESIDENT' THEN
UPDATE empnew SET sal = sal + 1000 WHERE CURRENT OF empnew_cursor;
ELSIF empnew_record.job = 'MANAGER' THEN
UPDATE empnew SET sal = sal + 500 WHERE CURRENT OF empnew_cursor;
ELSE
UPDATE empnew SET sal = sal + 300 WHERE CURRENT OF empnew_cursor;
END IF;
END LOOP;
COMMIT;
END;
SELECT * FROM empnew;
--使用OF子句在特定表上加行共用鎖
DECLARE
CURSOR empnew_cursor IS
SELECT d.dname dname,e.ename ename
FROM empnew e join dept d on e.deptno = d.deptno
WHERE e.deptno = &deptno
FOR UPDATE OF e.deptno;
BEGIN
FOR empnew_record IN empnew_cursor LOOP
DBMS_OUTPUT.PUT_LINE('部門名稱:'||empnew_record.dname||'員工名:'||empnew_record.ename);
DELETE FROM empnew WHERE CURRENT OF empnew_cursor;
END LOOP;
COMMIT;
END;
SELECT * FROM empnew where deptno = 20;