Update Top 1 record in table sql server

Update Top 1 record in table sql server

b’

WITH UpdateList_view AS (n  SELECT TOP 1  * from TX_Master_PCBA n  WHERE SERIAL_NO IN (0500030309) n  ORDER BY TIMESTAMP2 DESC n)nnupdate UpdateList_view nset TIMESTAMP2 = 2013-12-12 15:40:31.593n

Accepted answer of Kapil is flawed, it will update more than one record if there are 2 or more than one records available with same timestamps, not a true top 1 query.

n

    ;With cte as (n                    SELECT TOP(1) email_fk FROM abc WHERE id= 177 ORDER BY created DESC   n            )n    UPDATE cte SET email_fk = 10n

n

Ref Remus Rusanu Ans:- SQL update top1 row query

Update Top 1 record in table sql server

b’

UPDATE TX_Master_PCBAnSET TIMESTAMP2 = 2013-12-12 15:40:31.593,nG_FIELD=0000nWHERE TIMESTAMP2 IN n(n   SELECT TOP 1 TIMESTAMP2n   FROM TX_Master_PCBA WHERE SERIAL_NO=0500030309n   ORDER BY TIMESTAMP2 DESC   -- You need to decide what column you want to sort onn)n

Leave a Reply

Your email address will not be published.