SQL 2008 syntax

I have to revert the changes I made to one of the field s in the table.

my production database name db1 and backup database - db2 both have table customer with custnum indexed and field1 which I have to replace data in production with data from field1 of db2. I am looking for exact syntax.  
update db1 ........
Who is Participating?

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

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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
UPDATE db1.dbo.customer
SET field1 = (SELECT field1 FROM db2.dbo.customer WHERE db2.dbo.customer.custnum = db1.dbo.customer.custnum)

Open in new window

leop1212Author Commented:
I am getting errors
could you show same using alias db1 a , db2 b?

Msg 208, Level 16, State 1, Line 1
Invalid object name 'db2.customer'.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Of course db1 and db2 need to be replaced by the real names of the databases. As well the "fiedl1" should be replaced with the real column name since you didn't provide those name I just used some arbitrary names.
Here is the version with alias:
UPDATE db1.dbo.customer a
SET a.field1 = (SELECT b.field1 FROM db2.dbo.customer b WHERE b.custnum = a.custnum)

Open in new window

Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

leop1212Author Commented:
Msg 512, Level 16, State 1, Line 2
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Vitor MontalvãoMSSQL Senior EngineerCommented:
That means the custnum is not unique. You need to provide the primary key column(s) to assure the uniqueness of the row.
leop1212Author Commented:
there are two primary keys
Vitor MontalvãoMSSQL Senior EngineerCommented:
Ok, you have ONE primary key composed by TWO fields. So just need to add the second field in the WHERE clause to guarantee the uniqueness:
UPDATE db1.dbo.customer a
SET a.field1 = (SELECT b.field1 FROM db2.dbo.customer b 
                WHERE b.cust_num = a.cust_num AND b.cust_seq = a.cust_seq)

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
leop1212Author Commented:
thank you very much
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
Microsoft SQL Server 2008

From novice to tech pro — start learning today.