Click here to Skip to main content
15,923,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table say tbl_a with say 3 cols W,X,Y,Z. i can say the table data will be like this:
W	X	Y	Z
1	A	L	U
2	B	L	V
3	C	L	W
			
5	Null	L	U
6	Null	L	M
7	Null	L	Null


in this rows 5,6,7 are copied 1,2,3. So both data in all columns should be same. Buy unfortunately some mistake hapend and some records ended like this. Is there any query to copy the mismatch cols in a single stretch?

plz advice...
Posted
Updated 28-Jun-13 5:02am
v2
Comments
[no name] 28-Jun-13 1:38am    
Share your existing query first.?

What i know is to update columns one by one by using 2 temp tables for these 2 condiitons

SQL
select * into #a from tbl_a where w in(1,2,3)
select * into #b from tbl_a where w in(5,6,7)

update #b
set x=a.x,
   y=a.y,
   z=a.z
from #a a
where a.w+4=b.w
 
Share this answer
 
v2
To update records in a single query, try something like this:
SQL
UPDATE t1
   SET t1.x = t2.x
       t1.y = t2.y
       t1.z = t2.z
FROM (
    SELECT *, ROW_NUMBER() OVER(ORDER BY w) AS RowNo
    FROM table1
    WHERE w IN (5,6,7)
   ) AS t1 INNER JOIN (
    SELECT *, ROW_NUMBER() OVER(ORDER BY w) AS RowNo
    FROM table1
    WHERE w IN (1,2,3)
) AS t2 ON t1.RowNo = t2.RowNo
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900