Solved

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

Posted on 2016-09-26
5
46 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 29

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 29

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 50

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Viewers will learn how the fundamental information of how to create a table.

729 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