Update SQL

I am trying to update the value of a field in one table to be that of another field on the same DB but from another table when the ids are the same.

I will be using the query in other databases so I want to include the database name in it. Here is the query. I get an error that the object is invalid or that the object cannot be bound. I double checked the names and they are correct. But there must be a syntax error:

UPDATE [BDotSostrinImmigration].[dbo.Users]
SET [BDotSostrinImmigration].[dbo.users].[MailCareOf] = [BDotSostrinImmigration].[dbo.zzzBDotAddressesHome].[ResCareOf]
WHERE [BDotSostrinImmigration].[dbo.Users].[trackeruserid] = [BDotSostrinImmigration].[dbo.zzzBDotAddressesHome].[IndivID]
AND [BDotSostrinImmigration].[dbo.zzzBDotAddressesHome].[imported] =1

Open in new window

LVL 1
AleksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
change

].[dbo.Users]

to

].[dbo].[Users]

everywhere:
UPDATE [BDotSostrinImmigration].[dbo].[Users]
SET [BDotSostrinImmigration].[dbo].[users].[MailCareOf] = [BDotSostrinImmigration].[dbo].[zzzBDotAddressesHome].[ResCareOf]
WHERE [BDotSostrinImmigration].[dbo].[Users].[trackeruserid] = [BDotSostrinImmigration].[dbo].[zzzBDotAddressesHome].[IndivID]
AND [BDotSostrinImmigration].[dbo].[zzzBDotAddressesHome].[imported] =1

Open in new window

AleksAuthor Commented:
Part of it seems to work but even thought the fields are there and the names are correct I get this error:

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "BDotSostrinImmigration.dbo.zzzBDotAddressesHome.IndivID" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "BDotSostrinImmigration.dbo.zzzBDotAddressesHome.imported" could not be bound.
AleksAuthor Commented:
I don't understand why this is happening. But I can run this query and it returns the results:

SELECT [BDotSostrinImmigration].[dbo].[zzzBDotAddressesHome].[ResCareOf]
FROM [BDotSostrinImmigration].[dbo].[zzzBDotAddressesHome]

Open in new window


But when I run the update it marks:   [BDotSostrinImmigration].[dbo].[zzzBDotAddressesHome].[ResCareOf]  in red and says that it cannot be bound ???

This is the query as I have it:

UPDATE [BDotSostrinImmigration].[dbo].[Users]
SET [BDotSostrinImmigration].[dbo].[users].[MailCareOf] = [BDotSostrinImmigration].[dbo].[zzzBDotAddressesHome].[ResCareOf]
WHERE [BDotSostrinImmigration].[dbo].[Users].[trackeruserid] = [BDotSostrinImmigration].[dbo].[zzzBDotAddressesHome].[IndivID]
AND [BDotSostrinImmigration].[dbo].[zzzBDotAddressesHome].[imported] =1

Open in new window

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

AleksAuthor Commented:
I even tried something simpler, like this:

UPDATE  dbo.Users
SET     Users.MailCareOf = zzzBDotAddressesHome.ResCareOf
WHERE Users.trackeruserid = zzzBDotAddressesHome.IndivID

Open in new window


But it says that:  

Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "zzzBDotAddressesHome.IndivID" could not be bound.

What does that mean ?
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>The multi-part identifier "zzzBDotAddressesHome.IndivID" could not be bound.
you need to join the table in your Update SQL statement... like:

UPDATE a
SET a.[MailCareOf] = b.[ResCareOf]
From [BDotSostrinImmigration].[dbo].[Users] a inner join [BDotSostrinImmigration].[dbo].[zzzBDotAddressesHome] b
on a.[trackeruserid] = b.[IndivID]
where b.[imported] =1

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AleksAuthor Commented:
This works !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.