Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need UPDATE query to add data to table

Posted on 2009-04-01
6
Medium Priority
?
511 Views
Last Modified: 2012-05-06
I have to combine 2 tables that should have originally been just one table. The first table contained a ClientID (manually entered number) and about 100 columns that were part of a survey. The second table contained the sameClientID and about 60 more columns that were part of the same survey. So now I have a table with one ClientID and all 160 columns but I only have the old data for the first 100 columns . I need to create an UPDATE query that will add the remaining 60 columns data to the table based on the matching ClientID field. I havent figured out how to make this happen with an SQL Server query...please help.
0
Comment
Question by:myndwire
[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
6 Comments
 
LVL 4

Expert Comment

by:bleach77
ID: 24046667
UPDATE table160
SET column1=(SELECT column1 FROM table60 WHERE table60.ClientID = table160.ClientID)
WHERE table160.ClientID = table60.ClientID

Don't forget to backup your data before trying this. :)
0
 
LVL 4

Expert Comment

by:bleach77
ID: 24046733
That one is only for one column. You can add it in the SET query.
Between this is assumptions that all ClientID is unique in both table.
UPDATE table160
SET column1=(SELECT column1 FROM table60 WHERE table60.ClientID = table160.ClientID), column2=(SELECT column2 FROM table60 WHERE table60.ClientID = table160.ClientID)
WHERE table160.ClientID = table60.ClientID

Open in new window

0
 
LVL 28

Accepted Solution

by:
TextReport earned 2000 total points
ID: 24047102
IN SQL Server you can do this with the FROM clause to join your tables.
Cheers, Andrew

UPDATE tblA
SET ColA = tblB.ColA
   ,ColB = tblB.ColB
   ,ColC = tblB.ColC
FROM tblA INNER JOIN tblB ON tblA.ClientId = tblB.ClientID

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 4

Expert Comment

by:bleach77
ID: 24047563
TextReport solution should be much better than mine.  :)
0
 
LVL 22

Expert Comment

by:dportas
ID: 24049237
In SQL Server 2008 use the MERGE statement instead of UPDATE with a JOIN:

MERGE INTO TableA
USING TableB ON TableA.ClientId = TableB.ClientID
WHEN MATCHED THEN UPDATE SET
 col1 = TableB.col1,
 col2 = TableB.col2,
 col3 = TableB.col3 ;
 
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 24058919
Curious, there are two solutions that will wor, so, why hasn't this been accepted or responded to ?

Just in case... Do you still have the old tables ? Or is the clientid in the same new table twice and you need to merge / fix within the same table ?

You can always refer to tableA as an alias of TableB in the above examples, just need to put in a bit more qualification to pick up those rows which have the missing rows...

e.g. 2008 merge syntax

MERGE INTO TableA
USING TableA as TableB ON TableA.ClientId = TableB.ClientID and TableA.col1 is NULL and TableB.col1 is not NULL      -- assuming col1 is one of those 'missing' columns
WHEN MATCHED THEN UPDATE SET
 col1 = TableB.col1,
 col2 = TableB.col2,
 col3 = TableB.col3 ;


or general SQL syntax :

UPDATE tblA
SET ColA = tblB.ColA
   ,ColB = tblB.ColB
   ,ColC = tblB.ColC
FROM tblA
INNER JOIN tblA as tblB ON tblA.ClientId = tblB.ClientID AND tbla.ColA is NULL and tblb.ColA is not NULL
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 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