我有一个客户表,如图所示
基于选择结果,我正尝试基于它执行插入或更新操作
CREATE TABLE customers
(
customer_id number(10) NOT NULL,
customer_serial number(10)
);
我尝试做以下3个步骤
1.我正在根据customer_id从customers获取customer_serial
1.如果未找到数据,则插入到客户中(25是customer_serial
的硬编码值)
1.否则,如果找到数据,则进行更新(向现有customer_serial
添加10)
验证码:
MERGE INTO customers
USING (SELECT customer_serial
FROM customers
WHERE customer_id = 2) cust
WHEN NOT MATCHED
THEN
INSERT (customer_id, customer_serial)
VALUES (customer_id, 25)
WHEN MATCHED
THEN
UPDATE
SET customer_serial = cust.customer_serial + 10
WHERE
customer_id = 2;
2条答案
按热度按时间nbysray51#
我想你需要以下语法:
从
using
子句中定义的给定客户id(这里是2
)开始,查询在表中搜索相应的记录(即on
子句)。如果不存在这样的行,则when not matched
子句插入具有固定序列25
的id,否则when matched
子句递增现有序列。fiddle
6jygbczu2#
您缺少
ON
子句,可以将查询简化为:fiddle