Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
raju33
Starting Member
1 Post |
Posted - 2014-09-17 : 05:38:18
|
Using MSSQL 2000. I have a table with duplicate rows that needs to be updated. I have new values with similar table.Table A:UID MD_ID Type12 11796 3013 11796 3115 11796 4818 11556 30242 11556 30518 11552 146Table B:UID MD_ID Type22 11556 31223 11556 31512 11552 11315 11796 5018 11796 4541 11796 48Need to update values of Table A using Table B. Table B may or may not have same number of rows as Table A, if it has less number of values, I can leave the original value as it is and if it has more values then I can skip extra values.UID in both tables doesn't match so cannot use UID to match value.Result:UID MD_ID Type12 11796 5013 11796 4515 11796 4818 11556 31242 11556 31518 11552 113 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2014-09-17 : 07:07:04
|
[CODE]CREATE TABLE TableA (UID int, MD_ID int, Type int)insert TableASELECT 12, 11796, 30 union allSELECT 13, 11796, 31 union allSELECT 15, 11796, 48 union allSELECT 18, 11556, 302 union allSELECT 42, 11556, 305 union allSELECT 18, 11552, 146CREATE TABLE TableB (UID int, MD_ID int, Type int)insert TableBSELECT 22, 11556, 312 union allSELECT 23, 11556, 315 union allSELECT 12, 11552, 113 union allSELECT 15, 11796, 50 union allSELECT 18, 11796, 45 union allSELECT 41, 11796,48UPDATE TaSET Ta.Type= Tb.TypeFROM (SELECT * FROM ( SELECT *, ( SELECT COUNT(*) FROM TableA AS counter WHERE counter.MD_ID = MyTable.MD_ID AND counter.UID <= MyTable.UID ) AS rowNumber FROM TableA MyTable ) AS r1) TaJOIN ( SELECT * FROM ( SELECT *, ( SELECT COUNT(*) FROM TableB AS counter WHERE counter.MD_ID = MyTable.MD_ID AND counter.UID <= MyTable.UID ) AS rowNumber FROM TableB MyTable ) AS r1 ) Tb ON Ta.MD_ID = Tb.MD_ID and Ta.rowNumber = tb.rowNumberSELECT * FROM TableA[/CODE]--Chandu |
|
|
|
|
|
|
|