如何从表 1 中获取特定记录并将其插入表 2?

How to FETCH perticular records from table1 and INSERT into table2?

Image_1 是 "table_1:-Product"

图 2 是 "table_2:-User_detail"

现在, 我想 select 来自 table_1 (p_name,p_amount) 的数据,使用特定的 "p_id" 和 "prompt" 并插入到 table_2 (purchased_item,amount_total,due_date) 在特定的 select "u_id"

预期输出:- 如果我是来自 table_1 的 select "p_id=101" 和来自 table_2 的 "u_id=3676" 那么 我在 table_2 ---

中收到此更新

"row-1"

u_id:-3676
u_name:-Rv
email:-rv@gmail.com
purchased_item:-LED
amount_total:-5000
due_date:-sysdate

请帮帮我,这怎么可能? 用程序还是用触发器?

感谢先进。

我是新 pl/sql 学习者。

how this is possible? with procedure or with trigger?

我猜都不是。 UPDATE 就行了。

update user_detail d set
  (d.purchased_item, d.amount_Total, d.due_Date) =
  (select p.p_name, p.p_amount, sysdate
   from product p
   where p.p_id = :par_p_id
  )
where d.u_id = :par_u_id;

如果必须是程序,那么

create or replace procedure p_iud (par_p_id in product.p_id%type,
                                   par_u_id in user_detail.u_id%type
                                  )
as
begin
  update user_detail d set
    (d.purchased_item, d.amount_Total, d.due_Date) =
    (select p.p_name, p.p_amount, sysdate
     from product p
     where p.p_id = par_p_id
    )
  where d.u_id = par_u_id;
end;
/

称之为

begin
   p_iud(101, 3676);
end;
/

如果您想同时传递多个产品,一种选择是将它们作为具有逗号分隔值的字符串传递。然后程序将是

create or replace procedure p_iud (par_p_id in varchar2,
                                   par_u_id in user_detail.u_id%type
                                  )
as
begin
  update user_detail d set
    (d.purchased_item, d.amount_total, d.due_Date) =
    (select listagg(p.p_name, ',') within group (order by p.p_name), 
            sum(p.p_amount), 
            sysdate
     from product p
     where p.p_id in (select to_number(regexp_substr(par_p_id, '[^,]+', 1, level))
                      from dual
                      connect by level <= regexp_count(par_p_id, ',') + 1
                     )
    )
  where d.u_id = par_u_id;
end;
/

你会称它为

begin
   p_iud('101,301', 3676);
end;
/

可能的问题:由于 LISTAGG 连接所有这些产品名称,如果这样的 "long" 字符串对于 PURCHASED_ITEM 列来说太长,它将失败(您可以 SUBSTR 它,如果这是一个选项)