【问题标题】:PLS 00357 Error- Table, View or Sequence "txt.col1" not allowed in the contextPLS 00357 错误 - 上下文中不允许使用表、视图或序列“txt.col1”
【发布时间】:2018-10-31 00:04:46
【问题描述】:

我创建了一个存储过程。如果 col1col2 的值与员工匹配,我希望在该存储过程中插入员工的唯一记录。如果未找到,则将 col1col2col3 的值与 employee 匹配匹配然后插入值。如果在匹配所有这些列时也找不到,则使用另一列插入记录。 还有一件事我想通过传递另一列值来找到像 emp_id 这样的值列表,如果单个记录不能 ma​​tch 然后制作 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 时发现了一个错误。如何解决此问题,以及如何在使用 CASEIF ELSIF 语句时将值从暂存表插入到主表。你能帮我编译存储过程吗?

【问题讨论】:

  • 您的过程有两个循环关键字,但只有一个产生问题的结束循环。
  • 我总是发现整齐地排列代码会使其中一些问题更容易被发现。当我看到这个时,我做的第一件事是修复案例和缩进,然后不匹配的loop/end loop 立即跳了出来。这里还有其他问题,但您可以先不要让自己和维护您的代码的任何人的生活变得艰难。

标签: oracle stored-procedures plsql cursor


【解决方案1】:

由于我没有您的数据库可以使用,因此很难 100% 确定,但在我看来,这条线是

rc rc%rowtype;

应该说

rc txt%rowtype;

您已将游标v_rc 定义为返回txt%rowtype,并且与此游标一起使用的SQL 语句为select * from txt,但该数据类型与rc 的定义不一致。因此,您似乎需要更改 rc,如图所示。

看起来应该删除紧跟在exit when v_rc%notfound; 之后的LOOP 语句,因为之后没有任何东西会终止该循环。

此外,您对txt 表中的列有很多引用,例如IF txt.col1 = rc.col1。您不能以这种方式引用表中的值。我不太确定你想在这里做什么,所以我无法提出任何建议。

还有,声明

select col1 from tbl1
  Where EXISTS (select col1 from tbl1 where tbl1.col1 = rc.col1);

正在从数据库中选择一列,但没有将其放在任何地方。这应该是单例 SELECT (SELECT..INTO) 或游标。

还有一件事:你不能使用distinct *。您需要使用带有distinct 的列列表。

也许以下内容与您正在尝试做的事情很接近:

create or replace procedure sp_ex
AS
begin
  FOR rc IN (SELECT * FROM TXT)
  LOOP
    FOR t1 IN (SELECT *
                 FROM TBL1
                 WHERE TBL1.COL1 = rc.COL1)
    LOOP
      IF t1.COL1 = rc.COL1 AND
         t1.COL2 = rc.COL2
      THEN
        insert into main_table
          select *
            from txt
            where txt.col2 = rc.col2;
      ELSIF t1.col1 = rc.col1 AND
            t1.col2 = rc.col2 AND
            t1.col3 = rc.col3
      THEN 
        insert into main_table
          select *
            from txt
            where txt.col2 = rc.col2;
      ELSE
        insert into main_table
          select *
            from txt
            where txt.col4 = rc.col4;
      END IF;
    END LOOP;  -- t1
  END LOOP;  -- rc
end sp_ex;

祝你好运。

【讨论】:

  • tbl1 中的 col1 和 txt 中的 col1 都不同。假设 tbl1 中的 col1 为“abc”,txt 中的 col1 为“xyz”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-23
  • 2021-11-03
  • 2018-02-27
  • 2017-09-05
  • 1970-01-01
  • 2018-11-07
  • 2014-11-28
相关资源
最近更新 更多