Updating record from cursor oracle
Updating record from cursor oracle
After creating a sequence, you can use it to generate unique sequence numbers for transaction processing. PUT_LINE ('Initial sequence value: ' || TO_CHAR(seq_value)); -- The NEXTVAL value is the same no matter what table you select from -- You usually use NEXTVAL to create unique numbers when inserting data. NEXTVAL, 'Lynette', 'Smith'); -- If you need to store the same value somewhere else, you use CURRVAL INSERT INTO employees_temp2 VALUES (employees_seq.You can use CREATE TABLE employees_temp AS SELECT employee_id, first_name, last_name FROM employees; CREATE TABLE employees_temp2 AS SELECT employee_id, first_name, last_name FROM employees; DECLARE seq_value NUMBER; BEGIN -- Display initial value of NEXTVAL -- This is invalid: seq_value := employees_seq. CURRVAL, 'Morgan', 'Smith'); -- Because NEXTVAL values might be referenced by different users and -- applications, and some NEXTVAL values might not be stored in the -- database, there might be gaps in the sequence -- The following uses the stored value of the CURRVAL in seq_value to specify -- the record to delete because CURRVAL (or NEXTVAL) cannot used in a WHERE clause -- This is invalid: WHERE employee_id = employees_seq. CURRVAL INTO seq_value FROM dual; DELETE FROM employees_temp2 WHERE employee_id = seq_value; -- The following udpates the employee_id with NEXTVAL for the specified record UPDATE employees_temp SET employee_id = employees_seq.
You must declare an explicit cursor for queries that return more than one row.
A transaction is a series of SQL data manipulation statements that does a logical unit of work.
For example, two statements might credit one bank account and debit another.
Implicit cursors are managed automatically by PL/SQL so you are not required to write any code to handle these cursors.
However, you can track information about the execution of an implicit cursor through its cursor attributes. For more information, see "Counting Rows Affected by FORALL with the %BULK_ROWCOUNT Attribute".
You give the cursor a name and associate it with a specific query.
You can optionally declare a return type for the cursor, such as DECLARE my_emp_id NUMBER(6); -- variable for employee_id my_job_id VARCHAR2(10); -- variable for job_id my_sal NUMBER(8,2); -- variable for salary CURSOR c1 IS SELECT employee_id, job_id, salary FROM employees WHERE salary The cursor is not a PL/SQL variable: you cannot assign values to a cursor or use it in an expression.PL/SQL also conforms to the current ANSI/ISO SQL standard.In addition to static SQL discussed in this chapter, PL/SQL also supports dynamic SQL, which enables you to execute SQL data definition, data control, and session control statements dynamically.SQL DECLARE 2 CURSOR employee_cur IS 3 SELECT * FROM employee 4 FOR UPDATE OF salary; 5 BEGIN 6 FOR employee_rec IN employee_cur 7 LOOP 8 UPDATE employee 9 SET salary = 10000 10 WHERE CURRENT OF employee_cur; 11 END LOOP; 12 END; 13 / PL/SQL procedure successfully completed.SQL SELECT * FROM employee; ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION ---- ---------- ---------- --------- --------- ---------- ---------- --------------- 01 Jason Martin 25-JUL-96 25-JUL-06 10000 Toronto Programmer 02 Alison Mathews 21-MAR-76 21-FEB-86 10000 Vancouver Tester 03 James Smith 12-DEC-78 15-MAR-90 10000 Vancouver Tester 04 Celia Rice 24-OCT-82 21-APR-99 10000 Vancouver Manager 05 Robert Black 15-JAN-84 08-AUG-98 10000 Vancouver Tester 06 Linda Green 30-JUL-87 04-JAN-96 10000 New York Tester 07 David Larry 31-DEC-90 12-FEB-98 10000 New York Manager 08 James Cat 17-SEP-96 15-APR-02 10000 Vancouver Tester 8 rows selected.By extending SQL, PL/SQL offers a unique combination of power and ease of use.