troubleshooting Question

T-SQL copy from multiple tables using Primary Key

Avatar of alright
alrightFlag for United States of America asked on
Microsoft SQL Server 2005
3 Comments1 Solution348 ViewsLast Modified:
I have 2 tables in a production database. The tables are prodDB.tblInfo and prodDB.tblInfo2. The tables share a key with column name sysid. I am looking to copy records which share the same sysid based on the value of one of the column's in prodDB.tblInfo into a new, non-production database (testDB.tblInfo3).

Scaled down example:
tblInfo contains FirstName, LastName, State, sysid. tblInfo2 contains Zip, Phone, Email, sysid.

I would like to copy the columns of tblInfo.FirstName, tblInfo.LastName, tblInfo.State and tblInfo2.Zip, tblInfo2.Phone, tblInfo2.Email into testDB.tblInfo3; however, I only want records WHERE State = 'CO'.

USE testDB
SELECT FirstName, LastName, State, sysid
INTO tblInfo3
FROM prodDB.tblInfo
WHERE State = 'CO'

This works fine but I want to then grab the remainder of the info (Zip, Phone, etc) from tblInfo2 of records with the same sysid
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros