The Oracle stored procedure gets the data instance from the database
- 2020-06-07 05:28:38
- OfStack
How can I insert data obtained from table A into another table B?
(1) If table A and table B have the same structure (number of fields, type of corresponding fields, etc.), it can be used
insert B select * FROM A;
insert INTO B (field1 field2, field3) select A. field1, A. field2, A. field3 from A;
(2) for two tables if the field sample number 1, but several field when the structure of 1 sample (similar to a parent-child relationship), must use insert INTO B (field1 field2) select A. field1, A. field2 from A;
1. insert function is realized by using a cursor with parameters:
create OR REPLACE PROCEDURE GET_DATA(
-- Parameter list:
n_task_id IN number, -- task number
v_task_name IN varchar2, -- task name
v_name IN varchar2 -- Name
)
-----------------------------------------------
-- PROCEDURE: GET_DATA --
Content: Obtain qualified data from the data source table and insert it into the target data table: --
-- Argument: n_tas_id Task ID, --
-- v_task_namek task name, --
-- v_bdw_name constraint on data source table: Local network name --
-----------------------------------------------
IS
-- Insert row count control
i_count number (5);
-- Fetch data cursor: DATA_CUR (IN_NAME)
-- Parameter: Local network name: IN_NAME
CURSOR DATA_CUR(IN_NAME VARchar2) IS /** Note: the parameters are defined without precision **/
select *
FROM GET_DATA_SRC A
where A.NAME = IN_NAME;
BEGIN
-- Counter, which controls the number of rows inserted
i_count: = 0;
-- Loop inserts data
FOR MYCUR IN DATA_CUR(v_name) LOOP
insert INTO ABC(
ROW_ID,
TASK_ID,
TASK_NAME,
GET_DATA_DT,
CUST_ID,
ASSIGN_FLAG,
DEAL_DATE
)VALUES(
SEQ_KD.NEXTVAL,
N_TASK_ID,
V_TASK_NAME,
SYSDATE,
MYCUR.CUST_ID,
'N',
NULL
);
(1) If table A and table B have the same structure (number of fields, type of corresponding fields, etc.), it can be used
insert B select * FROM A;
insert INTO B (field1 field2, field3) select A. field1, A. field2, A. field3 from A;
(2) for two tables if the field sample number 1, but several field when the structure of 1 sample (similar to a parent-child relationship), must use insert INTO B (field1 field2) select A. field1, A. field2 from A;
1. insert function is realized by using a cursor with parameters:
create OR REPLACE PROCEDURE GET_DATA(
-- Parameter list:
n_task_id IN number, -- task number
v_task_name IN varchar2, -- task name
v_name IN varchar2 -- Name
)
-----------------------------------------------
-- PROCEDURE: GET_DATA --
Content: Obtain qualified data from the data source table and insert it into the target data table: --
-- Argument: n_tas_id Task ID, --
-- v_task_namek task name, --
-- v_bdw_name constraint on data source table: Local network name --
-----------------------------------------------
IS
-- Insert row count control
i_count number (5);
-- Fetch data cursor: DATA_CUR (IN_NAME)
-- Parameter: Local network name: IN_NAME
CURSOR DATA_CUR(IN_NAME VARchar2) IS /** Note: the parameters are defined without precision **/
select *
FROM GET_DATA_SRC A
where A.NAME = IN_NAME;
BEGIN
-- Counter, which controls the number of rows inserted
i_count: = 0;
-- Loop inserts data
FOR MYCUR IN DATA_CUR(v_name) LOOP
insert INTO ABC(
ROW_ID,
TASK_ID,
TASK_NAME,
GET_DATA_DT,
CUST_ID,
ASSIGN_FLAG,
DEAL_DATE
)VALUES(
SEQ_KD.NEXTVAL,
N_TASK_ID,
V_TASK_NAME,
SYSDATE,
MYCUR.CUST_ID,
'N',
NULL
);