2013-12-12 22 views
19

mia domandaAggiornamento Top 1 record nella tabella SQL Server

UPDATE TOP (1) TX_Master_PCBA 
SET TIMESTAMP2 = '2013-12-12 15:40:31.593' 
WHERE SERIAL_NO IN ('0500030309') 
ORDER BY TIMESTAMP2 DESC 

con serial_No Colonna nella tabella TX_Master_PCBA Ho 10 record, ma voglio aggiornare l'ultima TIMESTAMP2 a datetime corrente.

query precedente sta gettando errore:

Sintassi non corretta in prossimità della parola chiave 'TOP'.

+0

possibile duplicato di [? UPDATE TOP SQL con ORDER BY] (http://stackoverflow.com/questions/19584315/sql-update-top-with-order-by) – GolfWolf

+0

sebbene la domanda indichi SQL-SERVER, per chi ricerca la soluzione MySql c'è un modo più semplice e veloce 'UPDATE TX_Master_PCBA SET TIMESTAMP2 = NOW() WHERE SERIAL_NO = '050030309' ORDINA BY TIMESTAMP DESC LIMIT 1' –

+0

Possibile duplicato di [SQL update top1 row query] (http: // stackov erflow.com/questions/3860975/sql-update-top1-row-query) – fabriciorissetto

risposta

21
UPDATE TX_Master_PCBA 
SET TIMESTAMP2 = '2013-12-12 15:40:31.593', 
G_FIELD='0000' 
WHERE TIMESTAMP2 IN 
(
    SELECT TOP 1 TIMESTAMP2 
    FROM TX_Master_PCBA WHERE SERIAL_NO='0500030309' 
    ORDER BY TIMESTAMP2 DESC -- You need to decide what column you want to sort on 
) 
7

Quando TOP viene utilizzato con INSERT, UPDATE, MERGE o DELETE, le righe riferimento non sono disposti in qualsiasi ordine e la clausola ORDER BY non può essere direttamente conformi a queste istruzioni. Se è necessario utilizzare TOP per inserire, eliminare o modificare le righe in un ordine cronologico significativo, è necessario utilizzare TOP insieme a una clausola ORDER BY specificata in un'istruzione subselect.

TOP non può essere utilizzato nelle istruzioni UPDATE e DELETE nelle viste partizionate.

TOP non può essere combinato con OFFSET e FETCH nella stessa espressione di query (nello stesso ambito di query). Per ulteriori informazioni, vedere http://technet.microsoft.com/en-us/library/ms189463.aspx

31
WITH UpdateList_view AS (
    SELECT TOP 1 * from TX_Master_PCBA 
    WHERE SERIAL_NO IN ('0500030309') 
    ORDER BY TIMESTAMP2 DESC 
) 

update UpdateList_view 
set TIMESTAMP2 = '2013-12-12 15:40:31.593' 
13

accettato risposta di Kapil è difettosa, si aggiorna più di un record se ci sono 2 o più di un record disponibili con gli stessi timestamp, non una vera query principale 1 .

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

Rif Remo Ruşanu Ans: - SQL update top1 row query

+0

Cosa faccio se voglio utilizzare Join qui e impostare i valori da quella tabella? –

2

Per coloro che stanno trovando una soluzione sicura filo dare un'occhiata here.

Codice:

UPDATE Account 
SET sg_status = 'A' 
OUTPUT INSERTED.AccountId --You only need this if you want to return some column of the updated item 
WHERE AccountId = 
(
    SELECT TOP 1 AccountId 
    FROM Account WITH (UPDLOCK) --this is what makes the query thread safe! 
    ORDER BY CreationDate 
) 
3

Funziona bene anche ...

Update t 
Set t.TIMESTAMP2 = '2013-12-12 15:40:31.593' 
From 
(
    Select Top 1 TIMESTAMP2 
    From TX_Master_PCBA 
    Where SERIAL_NO IN ('0500030309') 
    Order By TIMESTAMP2 DESC 
) t 
Problemi correlati