Solved

How to update a column from another column

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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
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.

719 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