Solved

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

Posted on 2016-09-26
5
35 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
5 Comments
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal 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 17

Expert Comment

by:Pawan Kumar Khowal
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 45

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:ScottPletcher
ScottPletcher 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now