简体   繁体   中英

Update a table by using cursor

Hi I'm Unable to update column in jobs_new table from jobs table by using a cursor even I got correct output in dbms_output . It is updating that column with same value below is the code and please help.

SET SERVER OUTPUT ON;

DECLARE
  CURSOR c_emp IS
    SELECT
      a.job_id,
      a.max_salary,
      b.job_id
    FROM jobs a, jobs_new b
    WHERE

  a_jobid VARCHAR2(100);
  b_jobid  VARCHAR2(10);
  l_salary NUMBER;
BEGIN

  OPEN c_emp;

  LOOP
    FETCH c_emp INTO a_jobid, l_salary, b_jobid;
    -- enter code here
    EXIT WHEN c_emp%NOTFOUND;

    UPDATE jobs_new
    SET max_salary = l_salary
    WHERE b_jobid = a_jobid;

    dbms_output.put_line(l_salary);
  END LOOP;

  CLOSE c_emp;
END;
/

Your query seems to be incomplete, nothing is there after where condition,

Problem is here

update jobs_new set max_salary = L_SALARY where b_jobid = a_jobid;

it should be

update jobs_new set max_salary = L_SALARY where job_id = a_jobid;

But considering job_is as unique key, you don't have to use cursor for this operation. you can have a single Update statement as

update jobs_new a set a.max_salary = (select b.max_salary
                                        from jobs b 
                                       where b.job_id = a.job_id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM