Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Developer,

I have the two table "Tbl_ATemp" And "Tbl_BTemp"
I want to update first Table coloumn "B_Id" from ID of second table Tbl_BTemp.
The tran_id is same both table and ID coloumn is identity.
I will try cursor but not got result that i want

My table is....

Tbl_ATemp

Id Tran_Id B_Id
101 444 NULL
102 444 NULL
103 444 NULL
104 444 NULL

Tbl_BTemp

Id Tran_Id A_Id
1 444 NULL
2 444 NULL
3 444 NULL

After the Updating Table Tbl_Atemp I want to show Result as

Tbl_ATemp

Id Tran_Id B_Id
101 444 1
102 444 2
103 444 3
104 444 NULL

Its noncoff one - one record from both table and update coloumn.
Its greatful for me if any provide me quarry or any method to solved my quarry.

Regards,
Ravi Sharma
Posted
Updated 18-Jul-12 19:40pm
v2
Comments
Tejas Vaishnav 19-Jul-12 1:53am    
is there any unique data column which is common to both a and b

so we can i identify this id is going to update with this particular record.

in above example all record in a and b have same 444 so it is quite confusing.
Ravi Sharma 2 19-Jul-12 2:09am    
no there is no unique data coloum both table, thats challenging for me also.
there is multiple trans_Id in both table that i want to noncoff one -one row. This is solved by cursor ??
JakirBB 19-Jul-12 2:24am    
So..How will you decide which Id from 2nd table will go which row of first table?
Ravi Sharma 2 19-Jul-12 2:27am    
The first three rows of table is noncuff of first three second table of row and remaining if not match then its be null.this are done because i want to handle multiple transaction Id from my Reco system.

1 solution

Though it should not be a general scenario, I have devised a way. Hope it will solve your problem.

SQL
Select AId,BId
INTO #tmp
from
   (select Id as AId, ROW_NUMBER() over (order by Id) r from Tbl_ATemp) a
    join
   (select Id as BId, ROW_NUMBER() over (order by Id) r from Tbl_BTemp) b
    on a.r=b.r

UPDATE Tbl_ATemp
SET B_Id = #tmp.BId
FROM Tbl_ATemp INNER JOIN #tmp ON Tbl_ATemp.Id = #tmp.AId

DROP TABLE #tmp


Cheers :) ..
 
Share this answer
 
v2
Comments
Ravi Sharma 2 19-Jul-12 5:07am    
Thanks A lot dude....from above quarry my prob is solved
You rock man...............Thanks a lot.............

regards,
Ravi Sharma

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