Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Query to cross reference two text fields and update data

Posted on 2005-02-25
Medium Priority
Last Modified: 2012-05-05

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,


Question by:BigSi
  • 2
LVL 50

Assisted Solution

Lowfatspread earned 100 total points
ID: 13401731
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?

Author Comment

ID: 13401768
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.
LVL 13

Accepted Solution

KarinLoos earned 400 total points
ID: 13401774
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 )


Author Comment

ID: 13401830
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,


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question