--- Oracle database cursor

- Query for all employees of the employee number, name and position.
The DECLARE
- define a cursor
CURSOR emp_cursor the SELECT EMPNO the IS, ename, Job the FROM EMP;
v_empno emp.empno the TYPE%;
v_ename emp.ename the TYPE%;
v_job emp.job the TYPE%;
the BEGIN
- open a cursor, perform queries
the OPEN emp_cursor;
- - extract data
LOOP
FETCH emp_cursor INTO v_empno, v_ename, v_job;
DBMS_OUTPUT.PUT_LINE ( 'employee number:' || v_empno || ', name:' || v_ename || ', position:' || v_job);
- what when to exit the loop?
-% FOUND,% NOTFOUND
the EXIT the WHEN emp_cursor% NOTFOUND;
the END LOOP;
- close the cursor
the CLOSE emp_cursor;
the END;

- Query for all employees of the employee number, name and position.
The DECLARE
- define a cursor
CURSOR emp_cursor the SELECT EMPNO the IS, ename, Job the FROM EMP;
v_empno emp.empno the TYPE%;
v_ename emp.ename the TYPE%;
v_job emp.job the TYPE%;
the BEGIN
- open a cursor, execute a query
--OPEN emp_cursor ;
- detecting whether the cursor is open
the iF THEN emp_cursor% ISOPEN
the DBMS_OUTPUT.PUT_LINE ( 'the cursor was already opened');
the ELSE
the DBMS_OUTPUT.PUT_LINE ( 'cursor is not open');
the END the iF;

END;

- cursor FOR loop
the DECLARE
CURSOR emp_cursor the SELECT EMPNO the IS, ename, Job the FROM EMP;
the BEGIN
FOR LOOP emp_record the IN emp_cursor
the DBMS_OUTPUT.PUT_LINE ( 'employee number:' || emp_record.empno || ', Name:' || emp_record.ename || ', position:' || emp_record.job);
END lOOP;
END;

- cursor FOR loop referenced in subqueries
BEGIN
FOR emp_record the iN (the SELECT empno, ename, the job the FROM emp) lOOP
DBMS_OUTPUT.PUT_LINE ( 'employee number : '|| emp_record.empno ||', name: '|| emp_record.ename ||', position: '|| emp_record.job);
the END LOOP;
the 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;

- The employee number input by the user, to update the specified salary of employees, such as wages rose 100
- implicit cursor
the BEGIN
the UPDATE empnew the SET SAL + SAL = 100 & the WHERE EMPNO = NO;
the IF FOUND THEN the SQL%
dbms_output.put_line ( 'successfully modified wages');
COMMIT;
ELSE
dbms_output.put_line ( 'wages failed to modify');!
ROLLBACK;
END IF;
END;

SELECT * FROM empnew;

- Press titles employees a raise, president rose 1,000 yuan, up 500 yuan managers, other employees rose 300 yuan.
--1: business requirements to achieve a display of a cursor in a conventional manner
the DECLARE
- to define a cursor
CURSOR empnew_cursor the SELECT EMPNO the IS, the FROM Job empnew;
v_empno empnew.empno the TYPE%;
v_job empnew.job the TYPE%;
the BEGIN
- open a cursor
OPEN empnew_cursor;
- extracting data
LOOP
the FETCH empnew_cursor the INTO v_empno, v_job;
the IF v_job = 'PRESIDENT' THEN
the UPDATE empnew the SET SAL + SAL the WHERE EMPNO = v_empno = 1000;
ELSIF v_job = 'MANAGER' THEN
the UPDATE empnew the SET SAL + SAL = 500 = the WHERE EMPNO v_empno;
the ELSE
the UPDATE empnew the SET SAL + SAL = 300 = the WHERE EMPNO v_empno;
the END the IF;
the EXIT the WHEN empnew_cursor% NOTFOUND;
the END LOOP;
a COMMIT;
- Close the cursor
the CLOSE empnew_cursor;
the 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: When adding or deleting data using the cursor, when the cursor is defined by using the cursor FOR UPDATE clause can be extracted from the data row level locking
the DECLARE
- define a cursor
CURSOR empnew_cursor the SELECT EMPNO the IS, the FROM empnew Job FOR UPDATE;
the BEGIN
the FOR the IN LOOP empnew_cursor empnew_record
dbms_output.put_line (empnew_record.empno || '----' || empnew_record.job);
the IF empnew_record.job = 'PRESIDENT' THEN
the UPDATE empnew the SET = SAL + SAL. OF 1000 empnew_cursor the CURRENT the WHERE;
ELSIF empnew_record = .job 'MANAGER' THEN
the UPDATE empnew the SET SAL + SAL = 500. OF empnew_cursor the CURRENT the WHERE;
the ELSE
the UPDATE empnew the SET SAL + SAL = 300. OF empnew_cursor the CURRENT the WHERE;
the END the IF;
the END LOOP;
a COMMIT;
the END;

SELECT * FROM empnew;

--FOR UPDATE NOWAIT not wait for a lock, such as found in the data row has a locking operation, will not wait, immediately return
the DECLARE
- define a cursor
CURSOR empnew_cursor the SELECT EMPNO the IS, the FOR Job the FROM empnew the NOWAIT the UPDATE;
the BEGIN
the FOR LOOP empnew_record the IN empnew_cursor
dbms_output.put_line (empnew_record.empno || '----' || empnew_record.job);
the IF empnew_record.job = 'PRESIDENT' THEN
the UPDATE empnew the SET = SAL + SAL. OF 1000 empnew_cursor the CURRENT the WHERE;
ELSIF empnew_record.job = ' MANAGER 'THEN
the UPDATE empnew the SET SAL + SAL = 500. OF empnew_cursor the CURRENT the WHERE;
the ELSE
the UPDATE empnew the SET SAL + SAL = 300. OF empnew_cursor the CURRENT the WHERE;
the END the IF;
the END LOOP;
a COMMIT;
the 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;

 

 

 

 

 


Guess you like

Origin www.cnblogs.com/xiaomifeng1010/p/11117347.html