Solved

How to update a column from another column

Posted on 2015-02-04
4
140 Views
Last Modified: 2015-02-04
I have two tables that are identical. (See table definitions at the bottom of the post). They both contain the same data, except that in Table_2 the Employee number is null.
 So I need to set the employee number in table 2 to the employee number in table 1, where they match on the EmployeeId column. I tried the below query however it is wrong.


 UPDATE Table_2 SET Table_2.EmployeeNumber = Table_1.EmployeeNumber
INNER JOIN Table_1
ON Table_2.EmployeeId = Table_1.EmployeeId

Can someone help me out please?


CREATE TABLE [dbo].[Table_1](
      [EmployeeNumber] [nchar](10) NULL,
      [EmployeeName] [varchar](150) NULL,
      [EmployeeId] [int] NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Table_2](
      [EmployeeNumber] [nchar](10) NULL,
      [EmployeeName] [varchar](150) NULL,
      [EmployeeId] [int] NULL
) ON [PRIMARY]
0
Comment
Question by:brgdotnet
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 480 total points
ID: 40590321
Give this a whirl..
UPDATE t2
SET t2.EmployeeNumber = t1.EmployeeNumber
FROM Table_1 t1
   JOIN Table_2 t2 ON  t1.EmployeeId = t2.EmployeeId

Open in new window

0
 
LVL 9

Assisted Solution

by:shivkasi
shivkasi earned 20 total points
ID: 40590325
That should work!!
UPDATE Table_2
SET       Table_2.EmployeeNumber  =  Table_1.EmployeeNumber
FROM  Table_2 INNER JOIN Table_1   ON  Table_2.EmployeeId = Table_1.EmployeeId
0
 
LVL 77

Expert Comment

by:arnold
ID: 40590331
The obvious question, why would you want to have two tables having the exact same set of information? This is as you've wound up increases of loss of data integrity. Presumably, the reason you are trying to fix table_2 is that there are other things that depend on it.

Do you have a unique index dealing with Emplyee name, ID combination? You could do a insert into table_2 (employeenumber,employeename,employeeID)  (select employeenumber,employeename,employeeID from table1) on duplicate update employeenumber=Employeenumber


Your table definitions in both still allow nulls.

Since you are saying the tables are supposed to be identical, export the data from table 1 and table 2 (just in case you overlooked someone) into a csv.

Redefine the table no more nulls. Depending on how you add the data, if you must have two tables with identical information, add constraints/triggers when data is inserted/updated/deleted it will get to the other table,

Load the data from CSV using bcp into table one.
0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 40590377
Arnold, I am importing excel spreadsheet data into a sql server table, and then updating the second table based upon the first one. That is what is going on here. Jim Horn's  solution is elegant and works perfectly. Thank you Jim Horn !!!!!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

820 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