This is a very common requirement. When we insert a record into a table, if the record already exists (usually with the same primary key/other conditions), we should not insert the record repeatedly, instead, update the record.
Therefore, our original idea is generally to have such a stored procedure/method/SQL, first determine whether the record exists, and then decide whether to update or insert it.
The first thing to avoid is reading back and forth with a program. They should at least be processed together atomically, otherwise, you do not need to judge and then execute the command to increase the performance overhead of the application by adding a database access, and the correctness of the high concurrency cannot be guaranteed.
In reference 6, the following select xxx, if xxx is null insert, ELSE, and UPDATE are provided. Of course, there IS no error, however, from SQL Server 2008 onwards, MERGE operations are provided to handle such operations. Of course, they can also contain deletions, but here we only mention INSERTorUPDATE.
The following method is written by using a MongoDB method named Update + Insert = Upsert. In fact, the example provided by Microsoft also clearly describes this issue. Please refer to reference 1.
USE [MyDB]; goif exists (SELECT * FROM sys. objects WHERE object_id = OBJECT_ID (n' [dbo]. [UpsertUser] ') AND type in (n'p', n'pc') drop procedure [dbo]. [UpsertUser] GOUSE [MyDB]; goif exists (SELECT * FROM sys. objects WHERE object_id = OBJECT_ID (n' [dbo]. [# AnalysisForUpsertUser] ') AND type in (n'u') drop table [dbo]. [# creating] gocreate table # using (ExistingUserID int, ExistingUserName nvarchar (50), using nvarchar (50), ExistingPassword nvarchar (50), ExistingState bit, ExistingEmail nvarchar (50 ), existingUpdateBy nvarchar (50), ExistingUpdateTime, ExistingRemark nvarchar (50), ActionTaken nvarchar (10), NewUserID int, NewUserName nvarchar (50), NewUserFullName nvarchar (50 ), newPassword nvarchar (50), NewState bit, NewEmail nvarchar (50), NewUpdateBy nvarchar (50), NewUpdateTime datetime, NewRemark nvarchar (50),); GOSET primary ONGOSET primary ongocreate procedure dbo. upsertUser @ UserID int, @ UserName nvarchar (50), @ UserFullName nvarchar (50), @ Password nvarchar (50), @ State bit, @ Email nvarchar (50 ), @ UpdateBy nvarchar (50), -- @ UpdateTime datetime, @ Remark nvarchar (50) asbegin set nocount on; MERGE tbUser AS target USING (SELECT @ UserID, @ UserName, @ UserFullName, @ Password, @ State, @ Email, @ UpdateBy, @ Remark) AS source (UserID, UserName, UserFullName, Password, State, Email, UpdateBy, Remark) ON (target. userID = source. userID) when matched then update set UserName = source. userName, UserFullName = source. userFullName, Password = source. password, State = source. state, Email = source. email, UpdateBy = source. updateBy, UpdateTime = GETDATE (), Remark = source. remark when not matched then insert (UserID, UserName, UserFullName, Password, State, Email, UpdateBy, UpdateTime, Remark) VALUES (source. userID, source. userName, source. userFullName, source. password, source. state, source. email, source. updateBy, GETDATE (), source. remark) OUTPUT deleted. *, $ action, inserted. * INTO # AnalysisForUpsertUser; END; GOEXEC UpsertUser @ UserID = 6887, @ UserName = n'test6887 ', @ UserFullName = n' edit all tests ', @ Password = n' YCRrXZzNrNU = ', @ State = 1, @ Email = n' test3 @ microsoft.com', @ UpdateBy = n' PROGRAM ', @ Remark = n' you can view all projects and modify them. '; EXEC UpsertUser @ UserID = 6889, @ UserName = n' test6889', @ UserFullName = n' edit all test', @ Password = n' YCRrXZzNrNU = ', @ State = 1, @ Email = n' test3 @ microsoft.com ', @ UpdateBy = n' PROGRAM', @ Remark = n' to view and modify all projects. '; EXEC UpsertUser @ UserID = 6882, @ UserName = n' test6882', @ UserFullName = n' edit all test', @ Password = n' YCRrXZzNrNU = ', @ State = 1, @ Email = n' test3 @ microsoft.com ', @ UpdateBy = n' PROGRAM', @ Remark = n' to view and modify all projects. '; SELECT * FROM [MyDB]. [dbo]. [tbUser] SELECT * FROM # AnalysisForUpsertUserGO; drop table # AnalysisForUpsertUser; GO
References:
1. MERGE (Transact-SQL)
2. Use MERGE to insert, update, and delete data
3. OUTPUT clause (Transact-SQL)
4. Determine whether functions and stored procedures exist in the SQL database.
5, T-SQL Insert or update
6. Source Code: WebForm: Nearforums Forum v7.0 source code