Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyone know how to do an update query? 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
This is what I want to do:
I have 2 tables - EXSHOLOG & logexpo

I want to get all of the records in exsholog where 2 fields match logexpo, then update those records and put an X in column showx4.

Like this:
I want to update EXSHOLOG and SET EXSHOLOG.SHOWX4 = 'X'
where exsholog.custno = logexpo.custno
And exsholog.progcd = logexpo.progcd

Here is my query, and it works in MS Access but not in SQL Server.

UPDATE EXSHOLOG, LogExpo SET EXSHOLOG.SHOWX4 = 'X'
WHERE ((exsholog.custno=logexpo.custno And exsholog.progcd=logexpo.progcd))

Can someone please tell me how to fix this.


I suck doing SQL code.
 
Code:
UPDATE exsholog
SET showx4 = 'X'
FROM exsholog e JOIN logexpo l
  ON e.custno = l.custno AND e.progcd = l.progcd

--James
 
Thank you James! I messed with this all morning. Never thought of this.

It works, but why?

Where does the e and l come from? Are they ghost tables created on the fly?
 
As nread says, they are just aliases I have assigned to the two tables.

The reason your original query failed is because you can only have one table specified in the UPDATE clause. If you want to use two or more tables you have to use the FROM clause as per my example.

--James
 
So is it kinda like this:

UPDATE exsholog
SET showx4 = 'X'
FROM exsholog AS e JOIN logexpo AS l
ON e.custno = l.custno AND e.progcd = l.progcd

I added AS.
 
That's exactly what it is! The AS keyword is optional.

--James
 
Thanks to you both.

And thanks James for the query. I just tried it and it works great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top