Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to update a column from another column

Posted on 2015-02-04
4
Medium Priority
?
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1920 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 80 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 80

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differeā€¦
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

604 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