Solved

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

Posted on 2016-09-26
5
40 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 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 48

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

861 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