相同 Table 的合并语句不会插入新行但会更新 Oracle DB 中的现有行
Merge Statement for same Table does not insert a new row but updates existing row in Oracle DB
我正在尝试在名为 CONFIG
的 Oracle 12 数据库 table 上使用 MERGE
语句执行更新插入操作,其架构如下所示:
id | name | value
如果存在包含 name
'NON_TAXABLE_CODE' 的行,请将 value
更新为“400”,否则在其中插入一个新行。
第一次尝试
MERGE into CONFIG dest USING (SELECT id, name, value from CONFIG where name = 'NON_TAXABLE_CODE') src
ON (dest.name = src.name)
WHEN MATCHED THEN
UPDATE SET dest.value = src.value
WHEN NOT MATCHED THEN
INSERT (id, name, value) VALUES (src.id, src.name, src.value )
如果行存在,这将更新值,但不会插入新行(如果不存在行)
第二次尝试:
MERGE into CONFIG as dest USING VALUES (0, 'NON_TAXABLE_CODE', '400') as src (id, name, value)
ON dest.name = src.name
WHEN MATCHED THEN
UPDATE SET dest.value = src.value
WHEN NOT MATCHED THEN
INSERT (id, name, value) VALUES (src.id, src.name, src.value )
Error report -
SQL Error: ORA-02012: missing USING keyword
02012. 00000 - "missing USING keyword"
*Cause:
*Action:
第三次尝试:
MERGE into CONFIG as dest USING (select 0 as id, 'VERTEX_PRODTYPE_NON_TAXABLE' as name, '400' as value from DUAL) as src
ON dest.name = src.name
WHEN MATCHED THEN
UPDATE SET dest.value = src.value
WHEN NOT MATCHED THEN
INSERT (id, name, value) VALUES (src.id, src.name, src.value )
Error report -
SQL Error: ORA-02012: missing USING keyword
02012. 00000 - "missing USING keyword"
*Cause:
*Action:
有人能告诉我哪里错了吗?
谢谢。
问题是,如果不满足 where 子句,则 src 中的 select 不会 return 任何行。因此,当条件失败时,它既不会更新也不会插入。
为避免这种情况,您可以 select 来自对偶 table 的值。
MERGE INTO config dest USING (
SELECT 100 AS id,
'NON_TAXABLE_CODE' AS name,
400 AS value
FROM dual
)
src ON ( dest.name = src.name )
WHEN MATCHED THEN UPDATE SET dest.value = src.value
WHEN NOT MATCHED THEN INSERT (
id,
name,
value ) VALUES (
src.id,
src.name,
src.value );
这将在名称匹配时更新行,但在不匹配时插入硬编码值select从对偶中编辑。
我正在尝试在名为 CONFIG
的 Oracle 12 数据库 table 上使用 MERGE
语句执行更新插入操作,其架构如下所示:
id | name | value
如果存在包含 name
'NON_TAXABLE_CODE' 的行,请将 value
更新为“400”,否则在其中插入一个新行。
第一次尝试
MERGE into CONFIG dest USING (SELECT id, name, value from CONFIG where name = 'NON_TAXABLE_CODE') src
ON (dest.name = src.name)
WHEN MATCHED THEN
UPDATE SET dest.value = src.value
WHEN NOT MATCHED THEN
INSERT (id, name, value) VALUES (src.id, src.name, src.value )
如果行存在,这将更新值,但不会插入新行(如果不存在行)
第二次尝试:
MERGE into CONFIG as dest USING VALUES (0, 'NON_TAXABLE_CODE', '400') as src (id, name, value)
ON dest.name = src.name
WHEN MATCHED THEN
UPDATE SET dest.value = src.value
WHEN NOT MATCHED THEN
INSERT (id, name, value) VALUES (src.id, src.name, src.value )
Error report -
SQL Error: ORA-02012: missing USING keyword
02012. 00000 - "missing USING keyword"
*Cause:
*Action:
第三次尝试:
MERGE into CONFIG as dest USING (select 0 as id, 'VERTEX_PRODTYPE_NON_TAXABLE' as name, '400' as value from DUAL) as src
ON dest.name = src.name
WHEN MATCHED THEN
UPDATE SET dest.value = src.value
WHEN NOT MATCHED THEN
INSERT (id, name, value) VALUES (src.id, src.name, src.value )
Error report -
SQL Error: ORA-02012: missing USING keyword
02012. 00000 - "missing USING keyword"
*Cause:
*Action:
有人能告诉我哪里错了吗? 谢谢。
问题是,如果不满足 where 子句,则 src 中的 select 不会 return 任何行。因此,当条件失败时,它既不会更新也不会插入。
为避免这种情况,您可以 select 来自对偶 table 的值。
MERGE INTO config dest USING (
SELECT 100 AS id,
'NON_TAXABLE_CODE' AS name,
400 AS value
FROM dual
)
src ON ( dest.name = src.name )
WHEN MATCHED THEN UPDATE SET dest.value = src.value
WHEN NOT MATCHED THEN INSERT (
id,
name,
value ) VALUES (
src.id,
src.name,
src.value );
这将在名称匹配时更新行,但在不匹配时插入硬编码值select从对偶中编辑。