Solved

Developing a Strategy for updating a table from a master table.

Posted on 2016-09-26
5
41 Views
Last Modified: 2016-09-30
Thank you all in advance!

I have a legacy table that recorded user name as "John Doe".
However, the new database has a "UserId" Column in" tbl_AuditDetail" set to int data type. That "UserID" is in turn linked to a  "tbl_UserDetail" with columns  "UserId", "UserFirstName", "UserMiddleName", "UserLastName".

Any thoughts on how to insert a number into "tbl_AuditDetail" based on the "John Doe" data from the legacy table?

I have a Select i cooked up that isolates the last name so..what might work best?

1. Stored Proc consisting of A series of nested queries that pull back the int value and then use that as part of an update statement?
2. A Case Statement?
3. User function?

Im thinking this is going to take some time but wanted to ask incase someone had some guidance.

Thank you all in advance!

-vn
0
Comment
Question by:Victor Nares
[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
5 Comments
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 250 total points
ID: 41817057
Here you go the complete code you need for this..


---

UPDATE r 
	SET r.userId = x.UserId 
FROM
(
	SELECT p.UserId, SUBSTRING(UserName, 0, CHARINDEX(' ',UserName)) FirstName , SUBSTRING(UserName, CHARINDEX(' ',UserName)+1, LEN(UserName) - CHARINDEX(' ',UserName) ) LastName
	FROM legacyTable p
)r
CROSS APPLY
(
	SELECT TOP 1 UserID FROM tbl_AuditDetail a
	INNER JOIN tbl_UserDetail u
	ON a.UserId = UserId
	WHERE l.LastName = u.LastName
	AND l.FirstName = u.FirstName
)x

--

Open in new window

0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41817059
Also note that this is a one time activity. You just run the above script and you are done.

Bye ! Enjoy !
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 41817925
Can you rephrase your question? It is somehow confusing for me.
If possible post all tables names, columns and respective data types and give some data sample as well (not only the "John Doe").
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 41818937
Since it's so difficult to accurately split names, I'd go the other way around.  Concatenate the separate values in the new table and look for a match in the legacy table.
0
 

Author Closing Comment

by:Victor Nares
ID: 41823741
Thank you all for your comments and thoughts. I would up using Scott's suggestion to concentrate the full name in the destination table and then used Pawan Kumar Khowal suggestion of a Cross Apply.

Thanks again. I'll add the actual SQL as soon possible.

-vn
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.

730 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