Solved

How to update a column from another column

Posted on 2015-02-04
4
148 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 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 78

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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