Using for update, PL-SQL Programming

Using FOR UPDATE

If you declare a cursor which will be referenced in the CURRENT OF clause of an UPDATE or DELETE statement, you should use the FOR UPDATE clause to obtain an exclusive row locks. An illustration is as shown below:

DECLARE

CURSOR c1 IS SELECT empno, sal FROM emp

WHERE job = 'SALESMAN' AND comm > sal

FOR UPDATE NOWAIT;

The FOR UPDATE clause identifies the row which will be updated or deleted, then locks each & every row in the result set. This is helpful when you want to base an update on the existing values in a row. In that situation, you should make sure that the row is not changed by the other user before the update.

The elective keyword NOWAIT tells the Oracle not to wait if the table has been locked by the other user. The Control is immediately returned to your program so that it can do the other work before trying again to obtain the lock. If you omit the keyword NOWAIT, the Oracle waits until the table is available.

All rows are locked when you open the cursor, they are not liked fetched. The rows are unlocked when you commit or roll back the transaction. And hence, you cannot fetch from a

When querying the multiple tables, you can use the FOR UPDATE clause to lock up the row locking to the particular tables. The Rows in a table are locked only if the FOR UPDATE OF the clause refers to the column in that table. For illustration, the following query locks rows in the emp table but not in the dept table:

DECLARE

CURSOR c1 IS SELECT ename, dname FROM emp, dept

WHERE emp.deptno = dept.deptno AND job = 'MANAGER'

FOR UPDATE OF sal;

As the next illustration shows, you use the CURRENT OF clause in an UPDATE or DELETE statement to refer to the newest row fetched from a cursor:

DECLARE

CURSOR c1 IS SELECT empno, job, sal FROM emp FOR UPDATE;

...

BEGIN

OPEN c1;

LOOP

FETCH c1 INTO ...

...

UPDATE emp SET sal = new_sal WHERE CURRENT OF c1;

END LOOP;

Posted Date: 10/4/2012 5:20:41 AM | Location : United States







Related Discussions:- Using for update, Assignment Help, Ask Question on Using for update, Get Answer, Expert's Help, Using for update Discussions

Write discussion on Using for update
Your posts are moderated
Related Questions
Develop Data Business Intelligence Project Project Description: We are linking our Microsoft SQL Database to GoodData Business Intelligence. We are seeking somebody who has e

Oracle 10G new features:- Automatic Database Diagnostic Monitor System Advancements - these methods will provides several methods for extracting reports through the Automatic

Defining REF CURSOR Types To make cursor variables, you take 2 steps. At first, you define a REF CURSOR type, and then declare the cursor variables of that type. You can defin

DECLARE : This keyword signals the beginning of the declarative section of the PL/SQL block, that contains local declarations. The Items declared locally exist only within the

Assignments in pl/sql The Variables and constants are initialized every time a block or subprogram is entered. By default, the variables are initialized to NULL. Therefore, unle

Primary Key - SQL A PRIMARY KEY specification carries an implicit NOT NULL constraint on each column of the specified key. When more than one key constraint is required, the k

SQL Functions The PL/SQL uses all the SQL functions involving the following aggregate functions that summarize the whole columns of the Oracle data: GROUPING, AVG, COUNT, STDDE

THEO R Y OF SPECIAL CREATION - Life originated on the earth due to natural events by the super natural power. The biblical story of creation of world within six days was p

UTL_FILE: The Package UTL_FILE permits your PL/SQL programs to read & write operating system (OS) text files. It gives a restricted version of the standard OS stream file I/O,

Sequential Control Dissimilar to the IF and LOOP statements, the GOTO and NULL statements are not important to the PL/SQL programming. The configuration of PL/SQL is such that th