Solved

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

Posted on 2016-09-26
5
37 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 24

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 24

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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 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.

912 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

24 Experts available now in Live!

Get 1:1 Help Now