Re: INSERT ... WHERE NOT EXISTS ...
- From: Piet Linden <pietlinden@xxxxxxxxxxx>
- Date: Wed, 29 Apr 2009 22:44:09 -0700 (PDT)
On Apr 29, 7:04 pm, KH <K...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Hi,
I can't seem to get the syntax right for an insert query. In Sql Server I
would do this:
INSERT INTO [bla] (a, b, c)
SELECT 0, 'foo', NULL
WHERE NOT EXISTS(SELECT * FROM [bla] WHERE a = 0)
So, don't insert the new row if there's already a row where (a = 0).
But Access complains that there's no table in the SELECT query.
I tried a derived table to no avail, and this:
INSERT INTO [bla] (a, b, c)
SELECT TOP 1 0, 'foo', NULL
FROM [bla]
WHERE NOT EXISTS(SELECT * FROM [bla] WHERE a = 0)
but that doesn't work if [bla] has no rows.
Anybody know how to do that in Access? Thanks in advance!
- KH
Do you mean that you have two tables/sets which partially overlap
(contain some of the same records) and you just want to add the
missing ones from one set to the other?
If that's the case, the easiest way is to run the "Find unmatched"
query wizard and then turn that into an append query. What it amounts
to is something like this:
SELECT ...
FROM a LEFT JOIN b ON a.joinfield=b.joinfield
WHERE b.joinfield IS NULL
Then you turn that into an append query...
Or did I misunderstand what you were asking (wouldn't be the first
time...)
.
- References:
- INSERT ... WHERE NOT EXISTS ...
- From: KH
- INSERT ... WHERE NOT EXISTS ...
- Prev by Date: RE: On duplicate change value of other field
- Next by Date: Re: Check box results into a query
- Previous by thread: Re: INSERT ... WHERE NOT EXISTS ...
- Next by thread: Re: INSERT ... WHERE NOT EXISTS ...
- Index(es):
Relevant Pages
|