Oracle:如果一行不存在,如何插入
在 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'
)
相关文章