получить несколько строк, сохранить в 1 переменную и вставить в таблицу - PullRequest
0 голосов
/ 01 июня 2019

Я должен получить массовую запись и вставить в таблицу, используя цикл Я немного путаюсь, как извлечь и вставить запись с помощью цикла. Ниже я поделился тем, что сделал до сих пор.

declare
    stud_Id varchar;
begin            
    stud_Id := select student_id from student_backup where is_active_flg ='Y';

    for i in 1 ..stud_Id.count
    loop
        insert into users(student_id,password,status) values(stud_Id(i),'password','status')
        where not exists (select student_id from users where student_id=stud_Id(i))
    end loop;

    commit;
end;
/

Ответы [ 2 ]

2 голосов
/ 01 июня 2019

Вы можете использовать следующее:

declare
  stud_Id student_backup.student_id%type;
begin
     select nvl(max(student_id),0) into stud_Id 
       from student_backup 
      where is_active_flg ='Y';

   if stud_Id >0 then
    for i in 1 ..stud_Id
    loop
       insert into users(student_id,password,status) 
       select b.student_id,'password','status'
         from student_backup b 
         left join users u on b.student_id = u.student_id
        where is_active_flg ='Y'
          and b.student_id = i;
    end loop;
   end if; 
    commit;
end;
/

Демо

PS Если я понял, что вы хотите выступить, вы ненужно использовать цикл for (, включая оператор if ) и оператор select в начале, но непосредственно применить оператор вставки, удалив часть and b.student_id = i.Итак, конвертируйте ваш блок в следующий:

declare
  stud_Id student_backup.student_id%type;
begin

       insert into users(student_id,password,status) 
       select b.student_id,'password','status'
         from student_backup b 
         left join users u on b.student_id = u.student_id
        where is_active_flg ='Y' ;
    commit;
end;
/
0 голосов
/ 03 июня 2019

Абдул,

Я думаю, что вы ищете следующее:

BEGIN

INSERT INTO USERS 
SELECT STUDENT_ID, PASSWORD , STATUS
FROM student_backup
WHERE STUDENT_ID NOT IN (SELECT STUDENT_ID FROM USERS)
AND is_active_flg = 'Y';

END;
/

Надеюсь, это будет полезно.

Демо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...