View previous topic :: View next topic
|
Author |
Message |
rashmi jayakrishnan
New User
Joined: 28 Mar 2008 Posts: 3 Location: mysore
|
|
|
|
HI all
i have query regrading PL/SQL cursors
See the below examples
DECALRE CURSOR CUR_NEW IS
SELECT F_NAME from TBL_EMP WHERE AGE = 20;
for updating last name of those specific records ,we should give the same where condition in UPDATE command also. this is what i am thinking. please tell me whether i am right.
otherwise we can code like this
DECALRE CURSOR CUR_NEW IS
SELECT F_NAME form TBL_EMP WHERE AGE = 20
FOR UPDATE OF LAST_NAME;
and
UPDATE TBL_EMP SET LAST_NAME='kanjan'
when CURRENT OF CUJR_NEW
also tell me in the first approach if we havnt given the slection criteria that we used for UPDATE , what will happen?
whether it will point only the records selected in the cursor? |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Is there some reason to use a cursor?
Why select f_name? It does not appear to be used
Would a simple "update" ... "where age=20 do what you want?
There may be something that i do not understand correctly. . . |
|
Back to top |
|
|
gvsus
New User
Joined: 27 Mar 2008 Posts: 4 Location: USA
|
|
|
|
Why would you want to update all records with AGE = 20 to same LAST_NAME?
If thats what you really want to do, then you don't require a CURSOR, just use the Update.
Code: |
UPDATE TBL_EMP SET LAST_NAME='kanjan' WHERE AGE = 20; |
It will update all rows that have AGE = 20 with the LAST_NAME you specified.
If you want to use CURSOR for UPDATE, you still need to DECLARE, OPEN, FETCH that cursor before you can UPDATE any row. Once you FETCH a row then you can decide if you want to UPDATE that record or skip it and FETCH the next record.
But it is best to have the SELECTION criteria in the WHERE clause of the SELECT, so that you might not be locking up the rows that are not really needed for update.
Thanks |
|
Back to top |
|
|
|