在 PL/SQL (oracle) 中插入不存在的行的最简单方法是什么?
What is the easiest way to INSERT a row if it doesn't exist, in PL/SQL (oracle)?
我想要类似的东西:
IF NOT EXISTS (SELECT * FROM table WHERE name = 'jonny') THEN
INSERT INTO table VALUES ("jonny", null);
END IF;
但它不起作用.
注意:此表有 2 个字段,例如 name 和 age.但只有名字是PK.
Note: this table has 2 fields, say, name and age. But only name is PK.
INSERT INTO table
SELECT 'jonny', NULL
FROM dual -- Not Oracle? No need for dual, drop that line
WHERE NOT EXISTS (SELECT NULL -- canonical way, but you can select
-- anything as EXISTS only checks existence
FROM table
WHERE name = 'jonny'
)
这篇关于Oracle:如果一行不存在,如何插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!