views:

1535

answers:

3

I need to update a field (which is currently empty) based on a match with another table. This should be simple, but my syntax is wrong.

In SQLServer 2005, the syntax would be

UPDATE Facilities-NOID
SET Facilities-NOID.ID = Facilities-ID.ID
FROM Facilities-NOID, Facilities-ID
WHERE [Facilities-ID].[Structure ID] = [Facilities-NOID].[Structure ID];

Access doesn't like that FROM line, but if I leave it out, it wants each ID. This should be simple, but I'm not seeing it in the MS help on the Update syntax.

A: 

Remove Facilities-NOID from the FROM clause. It is redundant because you've already specified the table in the first line.

Edit: I just saw that you showed the Sql Server 2005 syntax. Why didn't you show the Access syntax?

Joe Philllips
I want to KNOW The Access syntax, so it's rather hard to show.UPDATE [Facilities-NOID] SET [Facilities-NOID].ID = [Facilities-ID].IDFROM Facilities-IDWHERE [Facilities-ID].[Structure ID]=[Facilities-NOID].[Structure ID];gives a syntax error (missing operator)
thursdaysgeek
If you don't want to know Jet SQL, why do you have an MS-ACCESS tag on the post? If you're using a passthrough query, then Access and Jet are not involved in the processing of the query, and simply aren't relevant -- it's then a strict SQL question.
David-W-Fenton
A: 

Remove the FROM clause completely

UPDATE Facilities-NOID
SET Facilities-NOID.ID = Facilities-ID.ID
WHERE [Facilities-ID].[Structure ID] = [Facilities-NOID].[Structure ID];

(that's the way that the query builder ends up constructing them)

CodeSlave
When I run the query, it asks for a parameter value. It wants Facilities-ID.ID. I want it to update the entire table with values from the other table.
thursdaysgeek
Have you tried putting brackets around your table names, as in [Facilities-ID].ID? I can't remember which characters Access/Jet doesn't like in names, but maybe the - (as opposed to underscore _) is treated the same as a space? I always use CamelCase, so never have any need for brackets.
David-W-Fenton
+1  A: 

I think this is what you want:

UPDATE Facilities-NOID 
INNER JOIN Facilities-ID ON Facilities-NOID.[Structure ID] 
    = Facilities-ID.[Structure ID]
SET Facilities-NOID.ID= Facilities-ID.ID

You are updating Facilities-NOID based on a match on Structure ID occurring in Facilities-ID.

Remou
Now it's asking for input a Facilities-ID.StructureID (no space!).Do I need some sort of select * from Facilities-ID?
thursdaysgeek
Ah, I had a typo! This did work. Thank you.
thursdaysgeek
The Jet query optimizer optimizes an explicit JOIN and an implicit join using a WHERE clause exactly the same, so there oughtn't be any difference between whether you use the WHERE clause or the INNER JOIN.
David-W-Fenton