Query to cross reference two text fields and update data


I have some data in an excel spread sheet for staff addresses. I also have a SQL database table with staff information on and am trying to come up with an automated process to import the data.

So far I have moved the excel data to a new temporary database table with all the columns of the excel file. I simply copied the excel data into the tempory SQL table.

My Existing Staff database table and the temporary table contain the following fields:-

Forename, Surname AddressLine1,AddressLine2,AddressLine3,TownCity,PostCode

Since the temporary table does not contain address records for ALL members of staff in my Staff database table I am unable to simply run a sort by forename and surname to copy and paste data.

I beleive the best way to update the data would be to cross reference both the Forename and Surname fields on the two tables and then update the adress data in table StaffData from that contained in TEMPTABLE

Does anyone know an update query (or anything similar) thatI can run to achieve this?

Thanks for reading,


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.

you'll be much better off with a join on an employee number are you sure
you havn't got several employees with the same name?
BigSiAuthor Commented:
There are no employees with the same Surname and Forename. I am unable to link using emplyee number since that data is not present in the excel file.

However, I have come up with a different sollution,

I created a 'make table' query that creates a new table based on the contents of the two tables being cross referenced.

I then plan to replace the table origional staff table with the newly created table.

I will post to say how I have got on.
I agree with Lowfat,  you would be better off 'crossreferencing' on the employeeID / employeeNumer , Social Security nr or someother totally unique field to an employee.
however  :
Update    StaffData
set     Addressline1  =  b.AddressLine1 ,
         AddressLine2  = b.AddressLine2,
         AddressLine3  = b.AddressLine3,
        TownCity         = b.TownCity,
         PostCode        = b.PostCode
FROM   StaffData                           a
JOIN    TEMPTABLE                        b ON (b.Forename = a.Forename
                                                     AND  b.Surname  = a.Surname )


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
BigSiAuthor Commented:
Thanks KarinLoos - The query worked a treat!

I never knew you could assign a table name an alias such as A and B so I've learnt something else there.

Thanks very much for you help!

It would have been usefull to have a single ID field in both tables that were the same, but the data was being imported from a seperate system that had no other field in common apart from forename and surname..

Thanks again,

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

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.