myndwire
asked on
Need UPDATE query to add data to table
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.
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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
TextReport solution should be much better than mine. :)
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 ;
MERGE INTO TableA
USING TableB ON TableA.ClientId = TableB.ClientID
WHEN MATCHED THEN UPDATE SET
col1 = TableB.col1,
col2 = TableB.col2,
col3 = TableB.col3 ;
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
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
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. :)