Condividi tramite


Implementazione della funzionalità MERGE

Potrebbe essere necessario eseguire in un database l'inserimento di un aggiornamento, a seconda se una determinata riga esiste già nel database.

Senza usare l'istruzione MERGE , di seguito è riportato un approccio che è possibile usare in Transact-SQL:

UPDATE mytable SET col=@somevalue WHERE myPK = @parm  
IF @@ROWCOUNT = 0  
    INSERT mytable (columns) VALUES (@parm, @other values)  

Un altro metodo Transact-SQL per implementare un merge:

IF EXISTS (SELECT 1 FROM mytable WHERE myPK = @parm)  
    UPDATE....  
ELSE  
    INSERT  

Per una stored procedure compilata in modo nativo

DECLARE @i  int  = 0  -- or whatever your PK data type is  
UPDATE mytable SET @i=myPK, othercolums = other values WHERE myPK = @parm  
IF @i = 0  
   INSERT....  

Vedere anche

Problemi di migrazione relativi alle stored procedure compilate in modo nativo
Costrutti Transact-SQL non supportati da OLTP in memoria