Transferred from: HTTP://HI.BAIDU.COM/TIDY0608/ITEM/FF930FE2436F2601560F1DD9
Sqlsever data exists on the update, there is no insertion of two methods
Two ways to use it frequently:
1. Update, if @ @ROWCOUNT = 0 Then insert
UPDATETable1 SETColumn1 = @newValue Whereid = @id
[email protected] @ROWCOUNT = 0
BEGIN
INSERT INTOTable1 (Id, Column1) VALUES (@id, @newValue)
END
If a row of data exists for the update to be valid, the insert will be executed. Personal feelings This approach tends to be the case where data is mostly present in the data table;
2. If row exists update, otherwise insert
Ifexists (select* FROMTable1 whereid = @id)
BEGIN
UPDATETable1 SETColumn1 = @newValue Whereid = @id
END
ELSE
BEGIN
INSERT INTOTable1 (Id, Column1) VALUES (@id, @newValue)
END
In this method, the SELECT statement executes immediately after execution of update or insert, and consumes less resources than method one.
3.mysql data exists on update, does not exist on insert
Insert into T (F1,F2,F3) VALUES (v1,v2,v3) on DUPLICATE KEY UPDATE v3=values (v3) +v3
SQL MySQL is present on update, and there is no notation for inserting (go)