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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update with Top 1 Record

Author  Topic 

QAZAFI
Yak Posting Veteran

50 Posts

Posted - 2008-05-15 : 01:13:12
I am in a situation in which I would like to update my one table three column with other table three column, The other table might have more then one record but I would like to have the TOP 1 record of that table for these column. How can I achive it. I know I will be able to achive by writing three statment like
Update abc
set a=(select top 1 a from xyz order by ),
b=( select top 1 b from xyz) and so but not sure that a and b using the same record and thats the requirment of update is
any help much apprecited

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-15 : 01:17:46
UPDATE a
SET Column1 = b.Column1, Column2 = b.Column2
FROM Table1 a
INNER JOIN (SELECT TOP 1 YourPK, Column1, Column2 FROM Table2 ORDER BY YourPK) b
ON a.YourPK = b.YourPK

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page
   

- Advertisement -