Oracle Update上的更新无效标识符

时间:2018-12-06 08:40:36

标签: oracle sql-update

运行下一个脚本时,出现“无效标识符”异常。 正如我在此link上看到的那样,它应该可以工作。

它不能引用选择项下的“ a2”表,但应使用相关值更新该行。

update auto a2 set uuid = 
    (select uuid from (
        select c.uuid, c.pk from color c
            join sit s on s.pk = c.sit_fk
            --where s.auto_fk = auto.pk
            join auto m on m.pk = s.auto_fk
            where m.pk = a2.pk
            group by c.pk, c.uuid
            order by c.pk desc
        )
    where rownum = 1)

1 个答案:

答案 0 :(得分:0)

在相关查询的外部级别而不是嵌套级别中引用a2别名。

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE auto ( pk, uuid ) AS
  SELECT 1, 1 FROM DUAL UNION ALL
  SELECT 2, 2 FROM DUAL;

CREATE TABLE color ( pk, uuid, sit_fk ) AS
  SELECT 1, 2, 1 FROM DUAL UNION ALL
  SELECT 2, 1, 2 FROM DUAL;

CREATE TABLE sit ( pk, auto_fk ) AS
  SELECT 1, 1 FROM DUAL UNION ALL
  SELECT 2, 2 FROM DUAL;

查询1

update auto a2 set uuid = 
    (select uuid from (
        select c.uuid, c.pk, m.pk AS apk from color c
            join sit s on s.pk = c.sit_fk
            --where s.auto_fk = auto.pk
            join auto m on m.pk = s.auto_fk
            group by m.pk, c.pk, c.uuid
            order by m.pk, c.pk desc
        )
    where rownum = 1
    and a2.pk = apk
)

Results

查询2

SELECT *
FROM   auto

Results

| PK | UUID |
|----|------|
|  1 |    2 |
|  2 |    1 |