简体   繁体   English

使用游标更新表

[英]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 . 嗨,我是无法更新列jobs_new从表jobs通过使用游标表,即使我得到了正确的输出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. 但是考虑到job_is是唯一键,您不必为此使用游标。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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