使用for循环我想在pl / sql代码中将数据从一个表复制到另一个表,如下所示

时间:2013-09-29 13:39:43

标签: sql plsql

此代码对我不起作用并给我留言并发送消息

ORA-01422       exact fetch returns more than requested number of rows  

代码:

declare  
  a_id employees.employee_id%type;  
  b_name employees.first_name%type;  
  c_salary employees.salary%type;  
begin  
  for i in 1..10 loop  
    select employee_id, first_name, salary into a_id, b_name, c_salary from employees 
    where employee_id='a_id', first_name='b_name', salary='c_salary';  

    insert into awais(employee_id, first_name, salary) values (a_id, b_name, c_salary);  
  end loop;  
end;  
/

2 个答案:

答案 0 :(得分:2)

看起来你的问题是你在a_id, b_name, c_salary中选择了多行。

您可以使用直接insert select

INSERT INTO awais
SELECT employee_id, first_name, salary into a_id, b_name, c_salary
  FROM employees 
 WHERE employee_id='a_id' 
   AND first_name='b_name' 
    AND salary='c_salary';

或者您可以使用游标进行循环:

declare  
  CURSOR c1
    IS
      select employee_id  a_id
           , first_name   b_name
           , salary       c_salary
        from employees 
       where employee_id='a_id' 
         AND first_name='b_name' 
         AND salary='c_salary'; 
begin  
  for i in 1..10 loop  
    for l_record in c1
    loop
      insert into awais(employee_id, first_name, salary) 
      values (l_record.a_id, l_record.b_name, l_record.c_salary); 
    end loop;


  end loop;  
end;  
/

答案 1 :(得分:0)

IIUC,您可以采取更简单的方法:

INSERT INTO awais
(SELECT employee_id, first_name, salary into a_id, b_name, c_salary
 FROM   employees 
 WHERE employee_id='a_id' AND first_name='b_name' AND salary='c_salary');