SQL 2005 Query copy between two tables

I have 2 tables that I need to do some copying with:
table hrdata will update table Customers
I need to copy some fields and combine 2 fields into 1 field
hrdata.Ansattnr will update Customers.CustomerID
hrdata.FirstName + hrdata.LastName will update Customers.ContactName
hrdata.Title will update Customers.ContactTitle

If hrdata.Ansattnr is not in Customers.CustomerID then it will be added as new record in Customers
If hrdata.Ansattnr is in Customers.CustomerID then it will be updated in Customers

Any help doing this as SQL script in SQL Server 2005 is highly appreciated.
LVL 10
TomMicrosoft ISV PartnerAsked:
Who is Participating?
tigin44Connect With a Mentor Commented:
-- update the records that are exixts in customers
SET C.ContactName = H.FirstName + ' ' + H.LastName ,
      C.ContactTitle = H.Title
FROM hrdata H INNER JOIN Customers C ON  H.Ansattnr = C.CustomerID

-- Insert the records that are not exists
INSERT INTO Customers (CustomerID, ContactName, ContactTitle)
SELECT H.Ansattnr, H.FirstName + ' ' + H.LastName, H.Title
FROM hrdata H
WHERE NOT EXISTS (SELECT CustomerID FROM Customers  C WHERE H.Ansattnr = C.CustomerID)
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.

All Courses

From novice to tech pro — start learning today.