使用复合主键合并到 table
Merge into table with composite primary key
我正在尝试将一些条目插入到我的 table 中,并且当条目已经存在时不执行任何操作。我的主键包含多个列(event_timestamp、device_id、路径、message_id)。
当我在 JDBC 中尝试类似以下语句时:
MERGE INTO events dest
USING ( SELECT event_timestamp, device_id, path, message_id from events) src
ON (dest.event_timestamp = src.event_timestamp
and dest.device_id = src.device_id
and dest.path = src.path
and dest.message_id = src.message_id)
WHEN NOT MATCHED THEN
INSERT (event_timestamp, device_id, path, message_id, text, direction, speed, net, gross)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
未插入任何内容。
使用像这样的普通语句:
INSERT (event_timestamp, device_id, path, message_id, text, direction, speed, net, gross) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
一切都插入没有问题。
我还尝试将 select 语句从 DUAL
更改为 select,而不是像 :
这样的事件
MERGE INTO events dest
"USING ( SELECT event_timestamp, device_id, path, message_id from DUAL) src
ON (dest.event_timestamp = src.event_timestamp
and dest.device_id = src.device_id
and dest.path = src.path
and dest.message_id = src.message_id)
WHEN NOT MATCHED THEN
INSERT (event_timestamp, device_id, path, message_id, text, direction, speed, net, gross)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
但是我得到了异常:ORA-00904: invalid identifier
我该怎么做才能让它发挥作用?
您要传递的新值应该是 USING
子句中 SELECT FROM dual
的一部分
MERGE INTO events dest USING (
SELECT ? as event_timestamp,
? as device_id,
? as path,
? as message_id
FROM dual
)
src ON (
dest.event_timestamp = src.event_timestamp
AND dest.device_id = src.device_id
AND dest.path = src.path
AND dest.message_id = src.message_id
)
WHEN NOT MATCHED THEN INSERT (
event_timestamp,
device_id,
path,
message_id,
text,
direction,
speed,
net,
gross ) VALUES (?,?,?,?,?,?,?,?,?);
这样的普通INSERT INTO .. SELECT
也可以用
INSERT INTO events (
event_timestamp,
device_id,
path,
message_id,
text,
direction,
speed,
net,
gross )
SELECT ?,?,?,?,?,?,?,?,? FROM DUAL
WHERE NOT EXISTS ( select 1 from events d WHERE
d.event_timestamp = ?
AND d.device_id = ?
AND d.path = ?
AND d.message_id = ?
);
我正在尝试将一些条目插入到我的 table 中,并且当条目已经存在时不执行任何操作。我的主键包含多个列(event_timestamp、device_id、路径、message_id)。
当我在 JDBC 中尝试类似以下语句时:
MERGE INTO events dest
USING ( SELECT event_timestamp, device_id, path, message_id from events) src
ON (dest.event_timestamp = src.event_timestamp
and dest.device_id = src.device_id
and dest.path = src.path
and dest.message_id = src.message_id)
WHEN NOT MATCHED THEN
INSERT (event_timestamp, device_id, path, message_id, text, direction, speed, net, gross)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
未插入任何内容。
使用像这样的普通语句:
INSERT (event_timestamp, device_id, path, message_id, text, direction, speed, net, gross) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
一切都插入没有问题。
我还尝试将 select 语句从 DUAL
更改为 select,而不是像 :
MERGE INTO events dest
"USING ( SELECT event_timestamp, device_id, path, message_id from DUAL) src
ON (dest.event_timestamp = src.event_timestamp
and dest.device_id = src.device_id
and dest.path = src.path
and dest.message_id = src.message_id)
WHEN NOT MATCHED THEN
INSERT (event_timestamp, device_id, path, message_id, text, direction, speed, net, gross)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
但是我得到了异常:ORA-00904: invalid identifier
我该怎么做才能让它发挥作用?
您要传递的新值应该是 USING
子句中 SELECT FROM dual
的一部分
MERGE INTO events dest USING (
SELECT ? as event_timestamp,
? as device_id,
? as path,
? as message_id
FROM dual
)
src ON (
dest.event_timestamp = src.event_timestamp
AND dest.device_id = src.device_id
AND dest.path = src.path
AND dest.message_id = src.message_id
)
WHEN NOT MATCHED THEN INSERT (
event_timestamp,
device_id,
path,
message_id,
text,
direction,
speed,
net,
gross ) VALUES (?,?,?,?,?,?,?,?,?);
这样的普通INSERT INTO .. SELECT
也可以用
INSERT INTO events (
event_timestamp,
device_id,
path,
message_id,
text,
direction,
speed,
net,
gross )
SELECT ?,?,?,?,?,?,?,?,? FROM DUAL
WHERE NOT EXISTS ( select 1 from events d WHERE
d.event_timestamp = ?
AND d.device_id = ?
AND d.path = ?
AND d.message_id = ?
);