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

Updating table 1 based on table 2

Status
Not open for further replies.

Einstein47

Programmer
Nov 29, 2001
737
US
Hey all,

I have an update like the following:
Code:
UPDATE table1 SET itemID = 1
 WHERE itemName = ( SELECT itemName FROM table2
                     WHERE itemID = 1 )

This works, but I have to modify the number each time and re-execute the SQL. Is there a way to have the SQL do the update for all values from table2?

Thanks in advance, Einstein47
("For every expert, there is an equal and opposite expert." - Arthur C. Clarke)
 
Try this:

update table1 join table2 on table1.itemName = table2.itemName set table1.itemID = table2.itemID
 
I got an error:
Code:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement.

If it is any help, I'm using an Access DB. Here is my SQL statement.
Code:
UPDATE tblPicks JOIN tblUsers ON
tblPicks.user = tblUsers.UserName
set tblPicks.userid = tblUsers.ID
Einstein47
("For every expert, there is an equal and opposite expert." - Arthur C. Clarke)
 
I don't have time to test this out but wouldn't the following work


Update tblPicks Set tblPicks.userid = tblusers.ID
Where tblPicks.user = tblUsers.UserName
 
Okay, so I tested that anyway and it doesn't work in Access. But, if you make it UPDATE tblPicks INNER JOIN tblusers, I think that might work.
 
Thanks - I'll give it a go - I'll let you know the results Einstein47
("For every expert, there is an equal and opposite expert." - Arthur C. Clarke)
 
I ended up using the following:
Code:
UPDATE tblPicks INNER JOIN tblUsers ON tblPicks.user = tblUsers.UserName
SET [tblPicks]![userid] = [tblUsers]![ID]
The square brackets may not be necessary, but for safety sake just incase you have a poorly named column (like me). Einstein47
("If computers ever get too powerful, we can organize them into a committee - that will do them in." - Unknown)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top