SQL insert from one table to another

Hi guys,

Need help with structure of an SQL insert. I will try to make the scenario clear....

I have a Customer table with a column called Country, which is an oid field, I have another column called OldCountry, which in an NVarChar. I have a seperate table called Country which contains an oid field and the name of the country. I need to loop through OldCountry looking for a 'string' match against the country table, if a match is found then return the oid from the country table and insert it into the Country oid column in Customer.

Hope thats relatively clear

Many Thanks,
Dean
deanlee17Asked:
Who is Participating?
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:
Do you want an INSERT or an UPDATE operation on you Customer table?

If it's an INSERT which are the values for the rest of columns?
If it's an UPDATE which record should be updates?

Anyway the following statement should gives all matches from Customer, Country and OldCountry tables. Can you check if it returns the correct records?

SELECT *
FROM Customer
INNER JOIN Country
    INNER JOIN OldCountry
        ON (Country.Name = OldCountry.Name)
    ON (Customer.Country = Country.ID)

Open in new window

0
deanlee17Author Commented:
Sorry my mistake, it is not an insert, it is an update on the Country field in the Customer table
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Ok. I also thought that.
Now I need to know which records should be updated.

And you ran my previous code? Got the expected results?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

deanlee17Author Commented:
Nope getting errors, Invalid Object Name 'OldCountry' just to be clear:

Table:   Customer
Fields:   oldCountry (varchar)
               country (UniqueIdentifier)

Table:   Country
Fields:   Name (varchar)
               oid (UniqueIdentifier)

The field we need to update is the Country field in Customer, so we need to update it with the oid's
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
I don't know the schema of your database so you need to change the table names and column names to yours.

So what are you saying me is that for a country name that exists in Customer and Country tables they can have different ID's so it's for update all Country ID's in Customer table with the OID from Country table, right?

If so then your update statement should be something like:

UPDATE Customer
SET country = (SELECT oid FROM Country WHERE Country.Name = Customer.oldCountry)

Open in new window

0

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
deanlee17Author Commented:
That looks like it could work, I will give it a try now...
0
deanlee17Author Commented:
Perfect, thanks.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Glad that I could help you.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.