带有错误 ORA-00904 的 Oracle 更新语句
Posted
技术标签:
【中文标题】带有错误 ORA-00904 的 Oracle 更新语句【英文标题】:Oracle update statement with error ORA-00904 【发布时间】:2020-07-07 03:16:46 【问题描述】:我只有两个简单的表格:
test1(id, name)
和
test2(id, name)
我想根据 test1 更新 test2 中的值。如果 test2 中的值不存在,则应插入新行。
我的查询:
MERGE INTO test2 trg
USING (
select c.id
from test1 c
) src ON (src.id = trg.id)
WHEN MATCHED THEN
UPDATE
SET
trg.name = src.name
WHEN NOT MATCHED THEN
INSERT (id)
VALUES (src.id);
但是这个查询给我带来了错误:
SQL 错误:ORA-00904:“SRC”。“NAME”:标识符无效 00904. 00000 - "%s: 无效标识符"
这是为什么?
表格:
create table test1
(
id number,
name varchar(10)
)
create table test2
(
id number,
name varchar(10)
)
insert into test1(id, name)
select 1, '1' from dual
union all select 2, '2' from dual
union all select 3, '3' from dual
union all select 4, '4' from dual
union all select 5, '5' from dual
union all select 6, '6' from dual
union all select 7, '7' from dual
union all select 8, '8' from dual
union all select 9, '9' from dual
union all select 10, '10' from dual
commit;
insert into test2(id, name)
select 20, '20' from dual
union all select 21, '21' from dual
union all select 22, '22' from dual
commit;
【问题讨论】:
【参考方案1】:MERGE INTO test2 trg
USING (
select c.id,c.name
from test1 c
) src ON (src.id = trg.id)
WHEN MATCHED THEN
UPDATE
SET
trg.name = src.name
WHEN NOT MATCHED THEN
INSERT (id,name)
VALUES (src.id,src.name)
【讨论】:
【参考方案2】:您在 MERGE 语句的 src
子查询中选择的唯一列称为 ID
。您还需要在该子查询中选择 NAME
列:
MERGE INTO test2 trg
USING (
select c.id, c.name
from test1 c
) src ON (src.id = trg.id)
WHEN MATCHED THEN
UPDATE
SET
trg.name = src.name
WHEN NOT MATCHED THEN
INSERT (id)
VALUES (src.id);
【讨论】:
以上是关于带有错误 ORA-00904 的 Oracle 更新语句的主要内容,如果未能解决你的问题,请参考以下文章
ORA-00904 Linq 在 Oracle 的 SQL 查询中生成错误
PL/SQL JAVA ORACLE 错误 ORA-00904: 标识符无效
Oracle DB - ORA-00904:选择时出现“无效标识符”错误
为啥我会收到:[Oracle][ODBC][Ora]ORA-00904:标识符无效
Oracle- 为啥在组合列中显示此错误“ORA-00904: "OUTLET_STATUS": invalid identifier"?