我有两张桌子:
CREATE TABLE
test
(
id INTEGER,
str VARCHAR2(10)
);
CREATE TABLE
test2
(
id INTEGER,
tmp VARCHAR2(10)
);
和内容:
INSERT INTO test2 (id, tmp) VALUES ( 10,'tmp10');
INSERT INTO test2 (id, tmp) VALUES ( 20,'tmp20');
当我运行下一个脚本时:
MERGE INTO test mt
USING (
SELECT
1 AS id,
10 AS id2
FROM
dual) nr
ON ( mt.id=nr.id)
WHEN MATCHED THEN
UPDATE SET str=(SELECT id FROM test2 WHERE id=nr.id2)
WHEN NOT MATCHED THEN
INSERT (id,str) VALUES ( nr.id,(SELECT tmp FROM test2 WHERE id=nr.id2) );
我看到错误:
[Error Code: 904, SQL State: 42000] ORA-00904: "NR"."ID2": invalid identifier
但是当我跑步时:
MERGE INTO test mt
USING (
SELECT
1 AS id,
10 AS id2
FROM
dual) nr
ON ( mt.id=nr.id)
WHEN MATCHED THEN
UPDATE SET str=(SELECT id FROM test2 WHERE id=nr.id2)
WHEN NOT MATCHED THEN
INSERT (id,str) VALUES ( nr.id,(SELECT tmp FROM test2 WHERE id=10) );
有用。
为什么新记录上的链接在更新语句中有效,但在插入语句中无效?