使用会话状态和另一个表中的值插入新表

时间:2017-11-06 11:38:50

标签: oracle oracle-apex

我想从procdure插入到如下所示的新表中 unit_id来自一个名为lms_units的表,它有一个名为qualification_id的FK到lms_qualifications 该表将包含1名具有1个资格的培训师以及随之而来的所有单元

insert into LMS_TRAINER_QUALI_UNITS
(
lms_trainer_quali_units_id,
Trainer_id,
Qualification_id,
unit_id
)
Values
(
lms_trainer_quali_units_id_seq.nextval,
:P2_Trainer_id,
:P6_qualification_id,
Unit_id --- this will have many values 
)

Oracle安装程序

CREATE TABLE lms_qualifications (
       Qualification_id, Title, Code
) AS
SELECT 33450, 'Lifter', 123 FROM DUAL;

CREATE TABLE LMS_UNITS ( 
       Unit_id, Qualification_id, description
) AS
SELECT 69052, 33450, 'Elective' FROM DUAL UNION ALL
SELECT 69053, 33450, 'core' FROM DUAL UNION ALL
SELECT 69054, 33450, 'core' FROM DUAL UNION ALL
SELECT 69055, 33450, 'Elective' FROM DUAL UNION ALL
SELECT 69056, 33450, 'Elective' FROM DUAL;

预计在LMS_TRAINER_QUALI_UNITS

+----------------------------+------------+------------------+---------+
| lms_trainer_quali_units_id | Trainer_id | Qualification_id | unit_id |       
+----------------------------+------------+------------------+---------+
| 50001                      |       4500 | 33450            | 69052   |
| 50002                      |       4500 | 33450            | 69053   |       
| 50003                      |       4500 | 33450            | 69054   |
| 50004                      |       4500 | 33450            | 69055   |       
| 50005                      |       4500 | 33450            | 69056   |
+----------------------------+------------+------------------+---------+

1 个答案:

答案 0 :(得分:0)

您可以使用INSERT INTO ... SELECT将值从一个表复制到另一个表:

insert into LMS_TRAINER_QUALI_UNITS (
  lms_trainer_quali_units_id,
  Trainer_id,
  Qualification_id,
  unit_id
)
SELECT lms_trainer_quali_units_id_seq.nextval,
       :P2_Trainer_id,
       Qualification_id,
       Unit_id
FROM   LMS_UNITS
WHERE  Qualification_id = :P6_qualification_id