【发布时间】:2018-10-31 00:04:46
【问题描述】:
我创建了一个存储过程。如果 col1 和 col2 的值与员工匹配,我希望在该存储过程中插入员工的唯一记录。如果未找到,则将 col1、col2 和 col3 的值与 employee 匹配匹配然后插入值。如果在匹配所有这些列时也找不到,则使用另一列插入记录。 还有一件事我想通过传递另一列值来找到像 emp_id 这样的值列表,如果单个记录不能 match 然后制作 emp_id 作为 NULL。
create or replace procedure sp_ex
AS
empID_in varchar2(10);
fname_in varchar2(20);
lname_in varchar2(30);
---------
type record is ref cursor return txt%rowtype; --Staging table
v_rc record;
rc rc%rowtype;
begin
open v_rc for select * from txt;
loop
fetch v_rc into rc;
exit when v_rc%notfound;
loop
select col1 from tbl1
Where EXISTS (select col1 from tbl1 where tbl1.col1 = rc.col1);
IF txt.col1 = rc.col1 AND txt.col2 = rc.col2 THEN
insert into main_table select distinct * from txt where txt.col2 = rc.col2;
ELSIF txt.col1 = rc.col1 AND txt.col2 = rc.col2 AND txt.col3 = rc.col3 THEN
insert into main_table select distinct * from txt where txt.col2 = rc.col2;
ELSE
insert into main_table select * from txt where txt.col4 = rc.col4;
end if;
end loop;
close v_rc;
end sp_ex;
我在编译这个存储过程PLS-00357: Table,View Or Sequence reference not allowed in this context 时发现了一个错误。如何解决此问题,以及如何在使用 CASE 或 IF ELSIF 语句时将值从暂存表插入到主表。你能帮我编译存储过程吗?
【问题讨论】:
-
您的过程有两个循环关键字,但只有一个产生问题的结束循环。
-
我总是发现整齐地排列代码会使其中一些问题更容易被发现。当我看到这个时,我做的第一件事是修复案例和缩进,然后不匹配的
loop/end loop立即跳了出来。这里还有其他问题,但您可以先不要让自己和维护您的代码的任何人的生活变得艰难。
标签: oracle stored-procedures plsql cursor