Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL update top1 row query

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; 
like image 776
Tina Avatar asked Oct 05 '10 05:10

Tina


People also ask

Can we update Top 100 rows in SQL?

UPDATE TOP (100) table_name set column_name = value; If you want to show the last 100 records, you can use this if you need. Show activity on this post. The TOP qualifier can also be used as limit the the number of rows manually updated incorrectly.

How do I update an entire row?

To update an entire row in MySQL, use UPDATE command. You need to know the primary key column. The syntax is as follows to update an entire row.

What is top1 in SQL?

The SQL SELECT TOP ClauseThe SELECT TOP clause is used to specify the number of records to return. The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact performance. Note: Not all database systems support the SELECT TOP clause.


2 Answers

With cte as ( select  top(1) shipfirtsname   From ShipBillInfo  where   CustomerId='134'  order by  OredrGUID desc) Update cte set shipfirstname='abc'; 
like image 97
Remus Rusanu Avatar answered Sep 20 '22 14:09

Remus Rusanu


why dont you do :

update ShipBillInfo  set shipfirstname='kkk'  where OrderGUID = (select top (1) OrderGUID                      from ShipBillInfo                     where CustomerId = 134                     order by OredrGUID desc ) 
like image 22
storm_buster Avatar answered Sep 23 '22 14:09

storm_buster