Click here to Skip to main content
15,924,317 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello Friends,
I have stuck in updating records. There are two tables
1. tbl_rate with column [rateid(PK),clientid,rvID(FK)]
2. tbl_rateversion with column [rvID(PK),clientid]

I want to update rvID of tbl_rate using condition
tbl_rate.clientid = tbl_rateversion.clientid


Any idea, how can i write query for this??

I have tried
update tbl_rate set rvID = (select distinct tbl_rateversion.rvID from tbl_rateversion
inner join tbl_rate on tbl_rate.clientid = tbl_rateversion.clientid) where ??


But not able to put where condition for it.

Thanks in advance
Posted
Comments
Ankur\m/ 21-Apr-11 2:57am    
You will probably have to use a loop here.
willempipi 21-Apr-11 3:11am    
I have the feeling you are making some really simple much more difficult than it is. Insert tbl_rateversion > read it's rvID > then use that to update your tbl_rate record... If I understood wrong please tell us more about what you are trying to do, like when is a tbl_rate record created, and the tbl_rateversion ect....

You can try something like:

SQL
update tr
    SET
      rvID = trr.rvID
FROM tbl_rate tr
    JOIN tbl_rateversion trr ON trr.clientid = tr.clientid
WHERE  . . . .(add other condition) ...


If you are using SQL 2008, then you can use Merge statement. Example:

http://blog.sqlauthority.com/2008/08/28/sql-server-2008-introduction-to-merge-statement-one-statement-for-insert-update-delete/[^]
 
Share this answer
 
v2
Comments
dhage.prashant01 21-Apr-11 3:49am    
Great help. . .
SQL
UPDATE tr
SET rvID = tresn.rvID
FROM tbl_rate tr
INNER JOIN tbl_rateversion tresn
ON tresn.clientid = tr.clientid
AND (Other Condition )
WHERE ?? 


More on this You can refer below link
[^]
 
Share this answer
 
v2

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