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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UPDATE statement with a join

Status
Not open for further replies.

thebign

Technical User
Nov 14, 2005
3
US
Hi everyone. I have two tables that are joined together by two fields - "Date" and "TrapID" (together they make up the primary key). I want to update a field ("DaysInField") in the 1st table based on a field ("DaysInField2") in the 2nd table, but only where a field ("Date2") in the 2nd table isn't NULL. I've tried 2 versions of code as pass-through queries in Access, and even though I don't recieve an error nothing gets updated. Any ideas?

I have a similar Access query that actually works. A png image of the design view can be seen here:
Version 1:
UPDATE tData
SET tData.DaysInField = tUpdateDaysInField.DaysInField2
FROM tData
INNER JOIN tUpdateDaysInField
ON (tData.Date = tUpdateDaysInField.Date) AND (tData.TrapID = tUpdateDaysInField.TrapID)
WHERE Not (tUpdateDaysInField.Date2 Is Null)


Version 2:
UPDATE tData
SET tData.DaysInField = tUpdateDaysInField.DaysInField2
FROM tData,tUpdateDaysInField
WHERE ((tData.Date = tUpdateDaysInField.Date) AND (tData.TrapID = tUpdateDaysInField.TrapID)) AND Not (tUpdateDaysInField.Date2 Is Null)
 
Try this:
Code:
UPDATE A
SET A.DaysInField = B.DaysInField2
FROM tData A
INNER JOIN  tUpdateDaysInField B
ON A.Date = B.Date AND A.TrapID = B.TrapID
WHERE B.Date2 IS NOT Null

-DNG
 
Hey thanks for the reply. Actually, I just found out that the problem was with the connection string to the database in the pass-through query, so the SQL statement itself was fine. I'm sure your code would work too, though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top