Solved

How to update a column from another column

Posted on 2015-02-04
4
124 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 76

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
back end of ssas cube views or tables? 2 27
How to calculate iops? 12 27
SQl query 19 12
IN with @variable 5 18
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now