Using cursor to update column, is this inefficient or illegal?

I have declared a cursor as follows in a PL/SQL procedure:
     cursor C is SELECT a FROM t
FOR UPDATE;
Later on in the procedure, I've written the following:
update t
set b = 'text'
where current of C;
My question is, am I allowed to do this when the column I am updating is not the one which I have selected in the cursor? If this is indeed legal, are there any performance issues involved?
Thanks,
Peter

Peter,
As it is confirmed by our folks that it is LEGAL to update columns that are not
fetched in the CURSOR, I am just going to talk about other things.
CURSOR is a read only resultset in PL/SQL which just gives you a handle to each of the rows that you are fetching from one or more table(s), based on the columns that you select in CURSOR. It is advised that you select the ID columns of the table(s) like PK if available from those tables so that you would not run into updating rows more than what is actually required. If we are not fetching the rows based on UNIQUE value and we use these values in UPDATE statements inside, we may get into trouble. Alternative and proves very good with performance is ROWID. I have used ROWID in CURSOR fetches and it works great.DECLARE
   CURSOR empCur IS
   SELECT ROWID unique_col, ename, job, sal FROM EMP
   WHERE sal > 1000;
BEGIN
   FOR empRec IN empCUR
   LOOP
      UPDATE emp SET job = 'Tech Lead'
      WHERE ROWID = empRec.unique_col;
   END LOOP;
END;Andrew,
Just curious, could you educate me on MVP status from Brainbech?
Thx,
SriDHAR

Similar Messages

  • PL/SQL Using Cursor to Update Another Cursor

    Dear all,
    i am trying to do the below process ..i just wrote this code as an example ..can anyone give me idea how to acheive
    this .. i attached the error msg i am getting below ..
    CREATE OR REPLACE PROCEDURE EMP_COMPARE
    AS
    CURSOR c_emp_err
    IS
    SELECT EMPNO FROM EMP_ERR;
    CURSOR c_emp_cur
    IS
    SELECT EMPNO,FRST_NAME,LAST_NAME
    FROM EMP WHERE DEPTNO=20;
    BEGIN
    FOR r_emp IN c_emp_cur
    LOOP
    IF r_emp.EMPNO NOT IN(c_emp_err) --DONT PICK THE EMP'S WHICH ARE IN ERROR TABLE
    THEN
    UPDATE EMP SET APPLY_DATE=SYSDATE
    WHERE EMPNO=r_emp.EMPNO;
    END IF;
    END LOOP;
    END;
    ERR: PLS-00320: the declaration of the type of this expression is incomplete or malformed
    Thanks in Advance ...

    Hi,
    I see.
    My answer still applies: don't use cursors if you don't have to. Look for a way to do what you need to using just SQL statements.
    If you really do have to use two cursors, where some values in one are taken from the other, you can have nested cursors.
    For exampe, using scott's tables, say you want to give a 10% salaray raise to everyone in any department based in a given city.
    You could have two cursors:
    (1) find each department based in the right city
    (2) find each employee in the current department from (1)
    CREATE OR REPLACE PROCEDURE     slow_update
    (     in_city_name     IN     VARCHAR2
    AS
         CURSOR     dept_csr     (target_loc     VARCHAR2)
         IS     SELECT     deptno
              FROM     dept
              WHERE     loc     = UPPER (target_loc);
         CURSOR     emp_csr     (target_deptno     NUMBER)
         IS     SELECT     empno
              FROM     emp
              WHERE     deptno     = target_deptno;
    BEGIN
         FOR     dept_rec IN dept_csr (in_city_name)
         LOOP
              FOR     emp_rec IN emp_csr (dept_rec.deptno)
              LOOP
                   UPDATE     emp
                   SET     sal     = sal * 1.1
                   WHERE     empno     = emp_rec.empno;
              END LOOP;
         END LOOP;
    END     slow_update;Notice how I used parameters in the cursors.
    You would run the procedure like this:
    EXECUTE  slow_update ('Dallas');Repeat: This is for demonstration purposes only.
    The smart way to do such an UPDATE doesn't require cursors or PL/SQL:
    UPDATE     emp
    SET     sal     = sal * 1.1
    WHERE     deptno     IN     (
                   SELECT     deptno
                   FROM     dept
                   WHERE     loc     = UPPER (:target_city_name)
                   );

  • How to update multiple columns from different tables using cursor.

    Hi,
    i have two tables test1 and test2. i want to udpate the column(DEPT_DSCR) of both the tables TEST1 and TEST2 using select for update and current of...using cursor.
    I have a code written as follows :
    DECLARE
    v_mydept1 TEST1.DEPT_CD%TYPE;
    v_mydept2 TEST2.DEPT_CD%TYPE;
    CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
    BEGIN
    OPEN C1;
         LOOP
              FETCH C1 INTO v_mydept1,v_mydept2;
              EXIT WHEN C1%NOTFOUND;
              UPDATE TEST2 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
              UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
         END LOOP;
         COMMIT;
    END;
    The above code when run says that it runs successfully. But it does not updates the desired columns[DEPT_DSCR].
    It only works when we want to update single or multiple columns of same table...i.e. by providing these columns after "FOR UPDATE OF"
    I am not sure what is the exact problem when we want to update multiple columns of different tables.
    Can anyone help me on this ?

    oops my mistake.....typo mistake...it should have been as follows --
    UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
    UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
    Now here is the upated PL/SQL code where we are trying to update columns of different tables --
    DECLARE
    v_mydept1 TEST1.DEPT_CD%TYPE;
    v_mydept2 TEST2.DEPT_CD%TYPE;
    CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
    BEGIN
    OPEN C1;
    LOOP
    FETCH C1 INTO v_mydept1,v_mydept2;
    EXIT WHEN C1%NOTFOUND;
    UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
    UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
    END LOOP;
    COMMIT;
    END;
    Please let us know why it is not updating by using using CURRENT OF

  • Update a column using cursor

    Hi everyone,
    still playing around with newbie stuff here. Is it possible to update a column or a row using a cursor? I understand how to fetch a data using a cursor, but not sure if it can help to update a data.
    If it can, what's the syntax?
    Regards,
    Valerie

    hi,
    why you opted to update rows by using cursor.
    why not you opt DML
    or
    just want to know
    SQL> SET LINE 32323
    SQL> /
    Rollback complete.
    SQL> SELECT *FROM EMP;
          7499 KITTU      SALESMAN        7698 20-FEB-81      15800      25050         30
          7566 JONES      MANAGER         7839 02-APR-81       3175     446.25         20
          7654 RAVIKUMAR  SALESMAN        7698 28-SEP-81       1450     1587.5         30 [email protected]
          7698 BLAKE      MANAGER         7839 01-MAY-81       3050      427.5         30
          7782 CLARK      MANAGER         7839 09-JUN-81       2650      367.5         10
          7788 SCOTT      ANALYST         7566 09-DEC-82       3200        450         20
          7839 KING       PRESIDENT            17-NOV-81       5200       8250         10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1700        225         30
          7876 ADAMS      CLERK           7788 12-JAN-83       1300        165         20
          7900 JAMES      CLERK           7698 03-DEC-81       1150       85.5         30 [email protected]
          7902 FORD       ANALYST         7566 03-DEC-81       3200        450         20
          7934 MILLER     CLERK           7782 23-JAN-82       1500        195         10
          8000 KINGBABA   PRESIDENT            17-NOV-81       5200       8250         10
          8001 TURNER RAV SALESMAN        8000 08-SEP-81       1700        450         30
    14 rows selected.
    SQL> DECLARE
      2  CURSOR CUR_EMP IS SELECT *FROM EMP;
      3  CUR_EMP1 CUR_EMP%ROWTYPE;
      4  BEGIN
      5  OPEN  CUR_EMP;
      6  LOOP
      7  FETCH CUR_EMP INTO CUR_EMP1;
      8  EXIT WHEN CUR_EMP%NOTFOUND;
      9  UPDATE EMP SET ENAME='TEST'
    10  WHERE ENAME=CUR_EMP1.ENAME;
    11  END LOOP;
    12  CLOSE CUR_EMP;
    13  END;
    14  /
    PL/SQL procedure successfully completed.
    SQL> SELECT *FROM EMP;
          7499 TEST       SALESMAN        7698 20-FEB-81      15800      25050         30
          7566 TEST       MANAGER         7839 02-APR-81       3175     446.25         20
          7654 TEST       SALESMAN        7698 28-SEP-81       1450     1587.5         30 [email protected]
          7698 TEST       MANAGER         7839 01-MAY-81       3050      427.5         30
          7782 TEST       MANAGER         7839 09-JUN-81       2650      367.5         10
          7788 TEST       ANALYST         7566 09-DEC-82       3200        450         20
          7839 TEST       PRESIDENT            17-NOV-81       5200       8250         10
          7844 TEST       SALESMAN        7698 08-SEP-81       1700        225         30
          7876 TEST       CLERK           7788 12-JAN-83       1300        165         20
          7900 TEST       CLERK           7698 03-DEC-81       1150       85.5         30 [email protected]
          7902 TEST       ANALYST         7566 03-DEC-81       3200        450         20
          7934 TEST       CLERK           7782 23-JAN-82       1500        195         10
          8000 TEST       PRESIDENT            17-NOV-81       5200       8250         10
          8001 TEST       SALESMAN        8000 08-SEP-81       1700        450         30
    14 rows selected.Edited by: user291283 on Sep 1, 2009 10:42 PM

  • UPDATE COLUMNS USING MAPPINGS

    I have to migrate data from one table to another where one of the columns is the same in both tables. e.g. ID COLUMN
    Is there a way to migrate data such that I can update all rows in the ID column in table 2 with corresponding values from ID column in table 1 using one update statement?
    i.e.
    TABLE 1 || TABLE 2
    ID || ID
    2 || 5
    5 || 9
    8 || 4
    34 || 98
    67 || 762
    89 || 99
    I want to use 1 update statement if possible to update all rows in ID column table 2 with values in ID column in table 1
    Thanks
    Edited by: 331991 on Feb 4, 2010 11:04 PM

    Hi,
    You can do this by using cursor.
    At First you have to save the id mapping relationship in a temp table, for example MAPPING_TABLE.
    Then you can do this as follow:
    Declare
        Cursor TO_UPDATE IS
        SELECT NEW.ID MEW_ID, NEW.COL1, NEW COL2,...., MAP.OLD_ID
        FROM NEW_TABLE NEW,
                  MAPPING_TABLE MAP
        WHERE NEW.ID=MAP.NEW_ID
        ORDER BY OLD_ID;
    Begin
        FOR REC IN  TO_UPDATE LOOP
             UPDATE OLD_TABLE
             SET ID=REC.NEW_ID,COL1=REC.COL1, COL2=REC.COL2, ....
             WHERE ID=REC.OLD_ID;
        END LOOP;
        COMMIT;
    End; The better way, that you may let a column for control issue.
    IF ATTRIBUTE1 is NULL for EACH ROW
    Declare
        Cursor TO_UPDATE IS
        SELECT NEW.ID MEW_ID, NEW.COL1, NEW COL2,...., MAP.OLD_ID
        FROM NEW_TABLE NEW,
                  MAPPING_TABLE MAP
        WHERE NEW.ID=MAP.NEW_ID
        ORDER BY OLD_ID;
    Begin
        FOR REC IN  TO_UPDATE LOOP
             UPDATE OLD_TABLE
             SET ID=REC.NEW_ID,COL1=REC.COL1, COL2=REC.COL2, ...., ATTRIBUTE1 = REC.OLD_ID
             WHERE ID=REC.OLD_ID
             AND ATTRIBUTE1 IS NULL;
        END LOOP;
        COMMIT;
    End; Best regards,
    Zhxiang
    Edited by: zhxiangxie on Feb 5, 2010 12:19 PM

  • How to pick max value from a column of a table using cursor and iteration

    Hello Everybody
    I have a table loan_detail
    and a column in it loan_amount
    now i want to pick values from this table using cursor and then by using iteration i want to pick max value from it using that cursor
    here is my table
    LOAN_AMOUNT
    100
    200
    300
    400
    500
    5600
    700i was able to do it using simple loop concepts but when i was trying to do this by using cursor i was not able to do it .
    Regards
    Peeyush

    SQL> SELECT MAX(sal) Highest_Sal,MIN(sal) Lowest_Sal FROM emp;
    HIGHEST_SAL LOWEST_SAL
           5000        800
    SQL> set serverout on
    SQL> DECLARE
      2    TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
      3    sal_tbl tmp_tbl;
      4    CURSOR emp_sal IS
      5      SELECT sal FROM emp;
      6    counter INTEGER := 1;
      7  BEGIN
      8    FOR i IN emp_sal LOOP
      9      sal_tbl(i.sal) := counter;
    10      counter := counter + 1;
    11    END LOOP;
    12    DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
    13    DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
    14  END;
    15  /
    Lowest SAL:800
    Highest SAL:5000
    PL/SQL procedure successfully completed.
    SQL> Even smaller
    SQL> DECLARE
      2    TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
      3    sal_tbl tmp_tbl;
      4    CURSOR emp_sal IS
      5      SELECT sal FROM emp;
      6    counter INTEGER := 1;
      7  BEGIN
      8    FOR i IN emp_sal LOOP
      9      sal_tbl(i.sal) := 1;
    10    END LOOP;
    11    DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
    12    DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
    13  END;
    14  /
    Lowest SAL:800
    Highest SAL:5000
    PL/SQL procedure successfully completed.
    SQL> Edited by: Saubhik on Jan 5, 2011 4:41 PM

  • Update Column value after current item is Approved and then publish major version using Sharepoint 2013 designer workflow

    Hi,
    We have a requirement to update a column value once the item has been approved.
    Following settings have been made in the publishing articles list:
    Require content approval for submitted items : yes
    Create major and minor (draft) versions
    Who should see draft items in this document library? :Only users who can edit items
    Require documents to be checked out before they can be edited? : yes
    I have createdatu a Sharepoint 2013 workflow to check if Approval sts of current item = 0 i.e. Approved , then check out and update the item and finally checkin the item. Everything works fine till this point except that the minor version of the item is
    checked in. Due to this the updated columns are not published to others.
    Also, I created a Sharepoint 2010 workflow to SET CONTENT APPROVAL = APPROVED and started this workflow from my list workflow above, but the item does not get checked-in and always shows "In Progress" status with comment "The item is currently
    locked for editing. Waiting for item to be checked in or for the lock to be released.".
    Please let me know where I am missing out so that once the item is approved, column value gets updated and current item is still in Approved status.
    Thanks

    Hi,
    According to your post, my understanding is that you want to update Column value after current item is Approved and then publish major version using Sharepoint 2013 designer workflow.
    You will get into this kind of Catch-22 situation trying to set the Content Approval Status in SharePoint Designer workflow:
    - You must check out the document before you can change the Content Approval Status
             - You can't change the Content Approval Status once the document in checked out
    Since you set the Require documents to be checked out before they can be edited=Yes, you will need to check out the document when run the workflow on the item. But you cannot approve a document when it is checked
    out. So the logic in workflow conflicts.
    As a workaround, you can use the Start Another Workflow action to start the normal Approval workflow on the document.  The built-in Approval workflow can work with a document that’s not checked out.
    The designer approval workflow also can work with a document that’s not checked out.
    You can create two workflow using SharePoint Designer 2013.
    First, create a SharePoint 2010 platform workflow.
    Then, create a SharePoint 2013 platform workflow.
    Then when the SharePoint 2013 platform workflow start, it will start the SharePoint 2010 platform workflow to set content approval status, then the SharePoint 2013 platform workflow will update current item value.
    More information:
    SharePoint Designer Workflow Content Approval Issue
    SharePoint 2010 Approval Workflow with Content Approval
    Best Regards,
    Linda Li
    Linda Li
    TechNet Community Support

  • HT4623 Please  help!  i have just update my iphone 4s with 7.4 update and my phone is now asking for a password which i dont have.  I have tried my keypad lock i used before the update and also my itunes password and neither work, how do i rectify this ??

    Please  help!  i have just update my iphone 4s with 7.4 update and my phone is now asking for a password which i dont have.  I have tried my keypad lock i used before the update and also my itunes password and neither work, how do i rectify this ???

    Did you buy this iPhone new from an authorized seller?

  • After trying to run the iTunes automatic update, I got this "The feature you are trying to use is on a network resource that is unavailable." Click OK to try again,or enter an alternate path to a folder containing the installation package 'itunes.msi"

    After trying to run the iTunes automatic update, I got this box on my pc "The feature you are trying to use is on a network resource that is unavailable. Click OK to try again, or enter an alternate path to a folder containing the installation package 'iTunes.msi" in the box below.. I ran a search for the file, and entered that into the box, to no avail. Then I launched iTunes and clicked on 'check for updates', and was told I had the latest version 11.0.1 I think it is. So, any clue on what to do? Thanks for any and all help.

    (1) Download the Windows Installer CleanUp utility installer file (msicuu2.exe) from the following Major Geeks page (use one of the links under the "DOWNLOAD LOCATIONS" thingy on the Major Geeks page):
    http://majorgeeks.com/download.php?det=4459
    (2) Doubleclick the msicuu2.exe file and follow the prompts to install the Windows Installer CleanUp utility. (If you're on a Windows Vista or Windows 7 system and you get a Code 800A0046 error message when doubleclicking the msicuu2.exe file, try instead right-clicking on the msicuu2.exe file and selecting "Run as administrator".)
    (3) In your Start menu click All Programs and then click Windows Install Clean Up. The Windows Installer CleanUp utility window appears, listing software that is currently installed on your computer.
    (4) In the list of programs that appears in CleanUp, select any iTunes entries and click "Remove", as per the following screenshot:
    (5) Quit out of CleanUp, restart the PC and try another iTunes install. Does it go through properly this time?

  • Using Cursor in Select statements? How to do this?

    I am getting an error whilt passing a cursor to a select clause:
    SELECT dbms_xmlquery.getXML('select deptno, dname, '||
    'cursor(select empno, ename, sal from emp e where e.deptno = d.deptno) employees '||
    'from dept d where d.deptno in (10, 20)')
    FROM dual;
    DBMS_XMLQUERY.GETXML('SELECTDEPTNO'||'CURSORIS(SELECTEMPNOFROMEMPEWHEREE.DEPT=D.
    <?xml version = '1.0'?>
    <ERROR>oracle.xml.sql.OracleXMLSQLException: ORA-00923
    : FROM keyword not found where expected
    </ERROR
    THIS IS DUE TO THE CURSOR AND ITS FROM STATEMENT?
    CAN ANY ONE PLEASE GUIDE AS TO HOW TO USE CURSORS IN A SELECT STATEMENT PLEASE?

    Another duplicate thread. See my response Select CLAUSE error using CURSORS & XSU.Please SEE..
    Cheers, APC

  • I just bought an iPhone 5, and tried to sync it to my iTunes account but when connected it says:  "...cannot be used because it requires iTunes version 10.7 or later." However when I try to get iTunes to update it says: "This version of iTunes (10.6.3) is

    I just bought an iPhone 5, and tried to sync it to my iTunes account but when connected it says:  “...cannot be used because it requires iTunes version 10.7 or later.” However when I try to get iTunes to update it says: “This version of iTunes (10.6.3) is the current version.” Can anyone help?

    filterc-r wrote:
    Thanks for your reply. My Mac is running 10.5.8: it's 4 years old.  When I try to update the OS software, I'm told there are none available.
    Be careful - there's a difference between update and upgrade.  Your machine is saying there are no updates available, meaning no later versions of OS X 10.5 (Leopard).  However, you can very likely upgrade - at least to OS X 10.6 (Snow Leopard), and possibly then to OS X 10.8 (Mountain Lion).
    Updates are free, but upgrades cost a bit.  That's the dirference.

  • The front camera on my iPod touch 4th generation is frozen. I cannot use it. Updating software does not help. This started right after I bought it, new from BestBuy. What can I do?

    The front camera on my iPod touch 4th generation is frozen. I cannot use it. Updating software does not help. This started right after I bought it, new from BestBuy. What can I do? When i go to the camera, it is stuck on a black screen. the only way to use my camera is through other camera apps, but if I try to switch it to the front camera, it freezes again.

    If you still have the problem after retoring the iPOd to factory defaults/new iPod then The iPod is likely defective and replacement is required.  You can make an appoinment at the Genius Bar of an APple store or take it back to BestBuy if within warranty

  • HT4623 When I try to update IOS 6 to my I Phone using my computer, it keeps saying  This version of iTunes (10.6.1) is the current version.  It won't let me update and I don't have wi fi

    When I try to update IOS 6 to my I Phone using my computer, it keeps saying  This version of iTunes (10.6.1) is the current version.  It won't let me update and I don't have wi fi

    It seems that you try to update iTunes rather than your phone. To update your phone, connect it to your computer, and on the page of your phone in iTunes, you should have a button "check for updates" or something similar.
    More info here: http://support.apple.com/kb/HT4623

  • Shuffle no longer syncs the playlists correctly and I cannot "see" the shuffle at all.  There is no "device" folder in the left column.  This may have occured after an Apple I-Tunes software update, but I am not sure. Software or hardware??

    Shuffle no longer syncs the playlists correctly and I cannot "see" the shuffle at all.  There is no "device" folder in the left column.  This may have occured after an I-Tunes software update, but I am not sure.  Is this likely a software issue or is the shuffle not working correctly?

    Shuffle no longer syncs the playlists correctly and I cannot "see" the shuffle at all.  There is no "device" folder in the left column.  This may have occured after an I-Tunes software update, but I am not sure.  Is this likely a software issue or is the shuffle not working correctly?

  • I bought iPhone 3GS(Used) recently and updated ios 6.1.3 my iPhone is taking blurry Pictures through basic default Cam app.Can anybody please let me know any solution for this?

    I bought iPhone 3GS(Used) recently and updated ios 6.1.3 my iPhone is taking blurry Pictures through basic default Cam app.Can anybody please let me know any solution for this?

    Basic troubleshooting steps right out of the user guide are restart, reset, restore from backup, restore as new.  If you've been through ALL the steps and you still have the problem, then you'll need to bring your phone to Apple for evaluation.

Maybe you are looking for