【发布时间】:2018-11-07 22:24:21
【问题描述】:
在我的存储过程中,我希望 col1 和 col2 的值与员工匹配,然后插入员工的唯一记录。如果未找到,则将 col1、col2 和 col3 的值与 employee 匹配匹配然后插入值。如果在匹配所有这些列时也找不到,则使用另一列插入记录。 我们必须使用 collection 以及 condition 然后 insert。 还有一件事我想通过传递另一列值来找到像 emp_id 这样的值列表,如果单个记录不能 match 然后制作 emp_id 作为 NULL。
我还想在与 txt 以及其他具有 emp 等数据的表匹配后一次插入一条记录。
create or replace procedure sp_ex
as
cursor c1 is select * from txt%rowtype;
v_col1 tbl1.col1%type;
type record is table of txt%rowtype; --Staging table
v_rc record := record();
begin
open c1;
loop
fetch c1 bulk collect into v_rc limit 1000;
loop
for i in 1..v_rc.count loop
select col1 into v_col1 from tbl1
where exists (select col1 from tbl1 where tbl1.col1 = emp.col1);
insert
when txt.col1 = emp.col1 and txt.col2 = stud.col2 then
into main_table(columns) values(v_rc(i).col1, ...)
when txt.col1 = emp.col1 and txt.col2 = stud.col2 and txt.col3 = stud.col3 then
into main_table(columns) values(v_rc(i).col1, ...)
else
insert into main_table(columns) values(v_rc(i).col1, ...)
select * from txt;
end loop;
exit when v_rc.count < limit;
end loop;
close c1;
end sp_ex;
而 emp,stud 是我必须与 txt 匹配的不同表格。 在那个存储过程中,我想以批处理模式将数据从 txt 加载到 main_table 中。数据将被一条一条记录匹配,然后如果匹配条件匹配则加载到主表中。 像 emp 表有 (col1, eid, ename, addr) 列和stud 表具有(col2、sid、sname、addr)。我如何创建存储过程,以便在批处理中通过上述逻辑一一加载数据。你能帮我分享你的想法吗?谢谢
【问题讨论】:
-
请提供tbl1和txt的create table语句。
-
@shahinP 如果您希望我们为您提供帮助,请向我们提供表格结构。如果没有结构,我们就无法判断问题发生在哪里。
-
你在代码中使用的emp是什么?请重新检查您的程序,有很多错误。
-
@VimalBhaskar emp, stud 是另一个表。我们将值与 txt 列匹配。
-
@shahinP 你能提供表格的“结构”吗?对两个表都使用 Desc 表名并将其放入问题中。
标签: sql oracle stored-procedures plsql