Append if rows do not exists else update if exists (2 Viewers)

jaryszek

Registered User.
Local time
Today, 07:53
Joined
Aug 25, 2016
Messages
756
Ok Guys i have one more question.

I would like to create new recordset from t_AnimalInput (UPSERT table but with condition).

So in one sql to do:
1. Check if Index equals 1 or 2 and take only these rows to create upsert query
2. Check if rows exist in t_AnimalSource and append if not
3. If exists just update rows in AnimalInput (but only with index 1,2 or just moved records)

How can i do this?
 

Attachments

  • Database8.accdb
    688 KB · Views: 122

jaryszek

Registered User.
Local time
Today, 07:53
Joined
Aug 25, 2016
Messages
756
I tried with:

Code:
UPDATE t_AnimalSource LEFT JOIN t_AnimalInput ON t_AnimalSource.Animal = t_AnimalInput.Animal SET t_AnimalInput.Animal = [t_AnimalSource].[Animal], t_AnimalInput.Color = [t_AnimalSource].[Color]
Where t_AnimalInput.[Index]=1

but it failed
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:53
Joined
May 7, 2009
Messages
19,248
It is just a name Microsoft gave to INSERT queries for no good reason.
Everyone is calling it append query not just microsoft?
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 00:53
Joined
Jan 20, 2009
Messages
12,852
Everyone is calling it append query not just microsoft?
The keyword in the "Append" query is INSERT. Other queries are named for their key word. SELECT, DELETE UPDATE.

Calling an INSET an Append is considered an Access quirk in the wider database fraternity. Same with calling a table column a "field". Even the SQL in Access calls them Columns.
 

isladogs

MVP / VIP
Local time
Today, 15:53
Joined
Jan 14, 2017
Messages
18,241
I'm slightly confused by your requirements as described in post #1.
Assuming I understand it correctly, this should partly work:
Code:
UPDATE t_AnimalSource LEFT JOIN t_AnimalInput ON t_AnimalSource.Animal = t_AnimalInput.Animal SET t_AnimalInput.Animal = [t_AnimalSource].[Animal], t_AnimalInput.Color = [t_AnimalSource].[Color]
WHERE (((t_AnimalInput.Index)=1 Or (t_AnimalInput.Index)=2));

However, as written it won't append new records from t_AnimalSource as the won't have a record with corresponding index in r_AnimalInput.
Shouldn't the index field be in the source table?
 

jaryszek

Registered User.
Local time
Today, 07:53
Joined
Aug 25, 2016
Messages
756
thank you Colin,

you pointed me in proper direction!
Yes,

this would not append record so i have to do the solution in 2 queries: 1 to append and one to update rows...

Best,
Jacek
 

isladogs

MVP / VIP
Local time
Today, 15:53
Joined
Jan 14, 2017
Messages
18,241
You're very welcome. Good luck with the new baby due tomorrow I believe.
 

Users who are viewing this thread

Top Bottom