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 2008 Forums
 Transact-SQL (2008)
 how to run an update statement just for first row

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2011-08-22 : 15:54:05
How can i run teh following update, all i want is to update the value just to first row of the table

can i do :
update top 1 @myTemptable set actualCost=@Actualcost

i have many rows within mytemptable, just want to update the value to first row.

thank you for the helpful info.

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2011-08-22 : 16:01:55
define first row, what makes it first date , an id or what? but something like this might work. always test

with cte(ActualCost, ProductUID)
As
(
SELECT TOP 1
FROM ActualCost, ProductUID
ORDER BY ProductUID ASC)

UPDATE tgt
SET tgt.actualCost=@Actualcost
FROM ActualCost tgt
inner join src
on tgt.ProductUID = src.ProductUID



If you don't have the passion to help people, you have no passion
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-22 : 23:45:07
even update top (1) should work

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-08-23 : 02:29:14
"even update top (1) should work"

which reminds me!

You could do:

SET ROWCOUNT 1
UPDATE ...
SET ROWCOUNT 0

to restrict the Update command to a single row - make sure you set ROWCOUNT back to zero though - otherwise all following commands will process only one row too!
Go to Top of Page

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2011-08-23 : 10:35:11
Visakh, Thank you very much. It worked good.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-23 : 11:28:54
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -