I have 2 tables. I need to update data the data in tableB, populate it with data from tableA.
But the problem is the design (note: I can’t change the design because its a ready made system. I’ll just have to deal with the fail).
Lets say tableA (2 columns) the data source is structured like so:
ID | NAME
1 dude
2 dud
3 bro
4 broo
5 killa
6 whale
--ID is an index and NAME is UNIQUE
Now tableB (6 columns) the destination (hard part for me):
ID1 | NAME1 | ID2 | NAME2 | ID3 | NAME3
1 dude 2 dud 3 bro
4 broo 5 killa 6 whale
--All columns here has UNIQUE data though it is not set on the design.
--and so on until the end.
Now what I want to do is UPDATE the names in tableA to the names in tableB corresponding to there ID’s.
Example:
ID from TableA is 1
Find ID=1 on TableB
If ID=1 then UPDATE the next column to 'dude'
Example end of TableA is:
ID | Name
9997 sweet
9998 jess
9999 grape
--The END
Example end of TableB is:
ID1 | NAME1 | ID2 | NAME2 | ID3 | NAME3
9997 sweet1 9998 jess1 9999 grape1
--The END
Please help me.
Note:
- All given table names and column names are for example purposes only.
- There are more columns in the tables, but they don’t need to be changed/altered so I did not include an example of them.
- The reason for this is that the data in
TableBhas been changed and is not matching the correct data anymore. - So I need to repopulate
TableBwith the correct data, and the only source for the correct data isTableA. - Lastly, I am using SQL Server 2008.
—– edit ——
Here is my final query that worked perfectly:
UPDATE CHAR_INFOR
SET
CHARID0=(SELECT CONVERT(VARCHAR,substring(char_data, 9, 16)) from CHAR_DATA0 A WHERE A.CHAR_KEY=CHAR0_KEY),
CHARID1=(SELECT CONVERT(VARCHAR,substring(char_data, 9, 16)) from CHAR_DATA0 A WHERE A.CHAR_KEY=CHAR1_KEY),
CHARID2=(SELECT CONVERT(VARCHAR,substring(char_data, 9, 16)) from CHAR_DATA0 A WHERE A.CHAR_KEY=CHAR2_KEY);
Seems like standard SQL should work: