tags:

views:

58

answers:

4

Can anybody please tell me how to write query to update top 1 record in table ?

Thanks

+1  A: 

There is no "top 1 record" in a relational table. Records are not in any order unless you specify one in the query.

Stefan Steinegger
+1  A: 

YOU have to decide what the top record is in a table, by ordering against a column you decide on.

That said, you could do this in SQL Server:

UPDATE [YourTable]
SET [YourColumn] = SomeValue
WHERE [PrimaryKey] IN 
(
   SELECT TOP 1 [PrimaryKey]
   FROM [YourTable]
   ORDER BY [PrimaryKey]  -- You need to decide what column you want to sort on
)
LittleBobbyTables
A: 

in SQLServer you can use the TOP keyword in your update expression:

http://msdn.microsoft.com/en-us/library/ms177523.aspx

When a TOP (n) clause is used with UPDATE, the update operation is performed on a random selection of 'n' number of rows.

Marcel de Kleine
A: 

Hi all

this is tina thanks for ur help,I m using SQL 2008

The query below is working:

update top(1) ShipBillInfo set shipfirstname='kkk' where CustomerId='134';

but it is showing error if i try to order by some Id: for example:

update top(1) ShipBillInfo set shipfirstname='kkk' where CustomerId='134' order by OredrGUID desc;

Plz help!

Thanks

Tina

Tina
Typo? OredrGUID
G Mastros
Take a look at LittleBobbyTables answer. By the way, you shouldn't write an answer just to add more information to your question.
Stefan Steinegger