Link to home
Start Free TrialLog in
Avatar of SpaceCoastLife
SpaceCoastLife

asked on

Trying to run an update query in SQL Server using 2 tables from 2 attached databases

--Sites

UPDATE db1.dbo.Post
SET db1.dbo.Post.GId = db1.dbo.routeGroup.SiteCode
FROM db1.dbo.Post
INNER JOIN db2
ON (db1.dbo.Post.CustomerId = db2.dbo.Meals_In.mls_prt_fk)

The result is all the references to db2 are underlined in red (invalid object name) even though I have verified both databases are attached and the names are correct.

Any suggestions anyone?
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>INNER JOIN db2
The INNER JOIN is expecting a table or view name, even if it's a fully qualified database_name.schema_name.table_or_view_name, and it appears that we have  just the database name db2 here.   You can't only list the database name, as SQL has no idea what table you're talking about and what column(s) to join.

>SET db1.dbo.Post.GId = db1.dbo.routeGroup.SiteCode
The table routeGroup is not defined in your FROM block.  

Using table aliases would make this much easier to read
Avatar of SpaceCoastLife
SpaceCoastLife

ASKER

Would you add your suggestions into my statement to make sure I understand?

As you can probably tell, I have very limited experience working with Sql Server - and I have no idea how to go about using aliases.

Would you also consider showing what the corrected version of my statement would look like with aliases? I'd very much appreciate it.
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help.