MS Access Insert where not exists

为君一笑 提交于 2019-12-02 01:09:15

You can't combine Values with a WHERE clause. You need to use INSERT INTO ... SELECT instead.

Since you don't want to insert values from a table, you need to use a dummy table. I use MSysObjects for that purpose (that's a system table that always exists and always contains rows):

INSERT INTO testTable (FirstName, Active) 
SELECT 'John', True
FROM (SELECT First(ID) From MSysObjects) dummy
WHERE NOT EXISTS (select 1 from testTable where FirstName='John' and Active=True)

In my case the field already exist in the table so I changed it from an INSERT to an UPDATE query and it worked.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!