oracle MERGE语句中出现无效标识符错误- PL/SQL

7gyucuyw  于 2023-05-22  发布在  Oracle
关注(0)|答案(2)|浏览(213)

我试图在PL/SQL中使用MERGE语句,但遇到了Invalid Identifier错误。

merge into t082assdetail t082
using (select * 
     from t082assdetail t082
     inner join t080assortment t080
     on t082.codassortment = t080.codassortment and t082.coddiv = t080.coddiv
     inner join tz084custcat tz084
     on t080.z_codbanner = tz084.z_codbanner and t080.codassortmenttype = tz084.codassortmenttype and t080.coddiv = tz084.coddiv
     and t082.z_custcodassortmenttype = t084.z_custcodassortmenttype) a
on (t082.codassortment = a.codassortment and t082.coddiv = a.coddiv and t082.prgassortment = a.prgassortment and t082.codart = a.codart
    and t082.numprg = a.numprg and t082.z_custcodassortmenttype = a.custcodassortmenttype)
when matched then update set t082.z_custcodassortmenttype = a.zcustcodassortmenttype

这是完整的查询,错误在a.prgassortment上触发。我相信问题是USING子句中使用的别名在外部无法识别,但我似乎找不到绕过它的方法。

41zrol4v

41zrol4v1#

你最初得到的异常是:

ORA-00904: "T084"."Z_CUSTCODASSORTMENTTYPE": invalid identifier

这是因为表别名是tz084而不是t084
一旦你解决了这个问题,你就会得到错误:

ORA-00904: "A"."Z_CUSTCODASSORTMENTTYPE": invalid identifier

这是因为您使用了SELECT *,并且在您要连接的表中有多个列具有相同的标识符;而是显式命名要选择的列,这样就不会有重复的标识符:

merge into t082assdetail t082
using (
  select t082.codassortment,
         t082.coddiv,
         t082.prgassortment,
         t082.codart,
         t082.numprg,
         t082.z_custcodassortmenttype
  from   t082assdetail t082
         inner join t080assortment t080
         on t082.codassortment = t080.codassortment
            and t082.coddiv = t080.coddiv
         inner join tz084custcat tz084
         on t080.z_codbanner = tz084.z_codbanner
            and t080.codassortmenttype = tz084.codassortmenttype
            and t080.coddiv = tz084.coddiv
            and t082.z_custcodassortmenttype = tz084.z_custcodassortmenttype
) a
on (   t082.codassortment = a.codassortment
   and t082.coddiv = a.coddiv
   and t082.prgassortment = a.prgassortment
   and t082.codart = a.codart
   and t082.numprg = a.numprg
   and t082.z_custcodassortmenttype = a.z_custcodassortmenttype
   )
when matched then
  update
  set t082.z_custcodassortmenttype = a.z_custcodassortmenttype

然后你会得到错误:

ORA-38104: Columns referenced in the ON Clause cannot be updated: "T082"."Z_CUSTCODASSORTMENTTYPE"

你不需要在ON子句中使用"T082"."Z_CUSTCODASSORTMENTTYPE",但这不是我们可以告诉你如何修复的,因为你需要弄清楚你还可以在什么地方连接表。
fiddle

mzaanser

mzaanser2#

我怀疑星号是罪魁祸首,因为它选择了所有相关表中的所有列。关于JOIN与具有相同名称的列 * 匹配 *-例如

and t082.z_custcodassortmenttype = t084.z_custcodassortmenttype     --> two z_custcodassortmenttype columns

这意味着select也返回了

select t082.z_custcodassortmenttype, t084.z_custcodassortmenttype   --> two z_custcodassortmenttype   columns

当你试图更新目标表的z_custcodassortmenttype列时,你会选择哪一个?当然,它们是匹配的,但这其中有歧义。
下面是一个例子:我想从DEPT表中填充T_EMP.DNAME列值:

SQL> select * from t_emp;

    DEPTNO ENAME      DNAME
---------- ---------- --------------------
        20 SMITH
        20 JONES
        10 CLARK
        20 SCOTT
        10 KING
        20 ADAMS
        20 FORD
        10 MILLER

8 rows selected.

SQL> select * From dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

带星号的merge(第2行)引发您提到的错误:

SQL> merge into t_emp a
  2    using (select *
  3           from emp e join dept d on d.deptno = e.deptno
  4          ) x
  5    on (a.deptno = x.deptno)
  6    when matched then update set
  7      a.dname = x.dname;
  on (a.deptno = x.deptno)
                 *
ERROR at line 5:
ORA-00904: "X"."DEPTNO": invalid identifier

但是,如果您显式地声明所涉及的列,则查询工作:

SQL> merge into t_emp a
  2    using (select distinct d.deptno, d.dname
  3           from emp e join dept d on d.deptno = e.deptno
  4          ) x
  5    on (a.deptno = x.deptno)
  6    when matched then update set
  7      a.dname = x.dname;

8 rows merged.

结果:

SQL> select * From t_emp;

    DEPTNO ENAME      DNAME
---------- ---------- --------------------
        20 SMITH      RESEARCH
        20 JONES      RESEARCH
        10 CLARK      ACCOUNTING
        20 SCOTT      RESEARCH
        10 KING       ACCOUNTING
        20 ADAMS      RESEARCH
        20 FORD       RESEARCH
        10 MILLER     ACCOUNTING

8 rows selected.

相关问题