MYSQL data insertion time, if there is a method of updating share

Abstract: The
following describes in MSSQL, insert the data, if there are updates, or share data on the method of inserting the
test environment: SQL Server 2017
MSSQL, we can MERGE INTO keyword adopt to implement this feature,
when the two match is successful, *** run statement, statements or other operation, to insert data operation is determined,
the specific operation method is as follows:

复制代码
create table [maomao365.com]
(keyId int identity,
info varchar(80)
)
go
insert into [maomao365.com]
(info)values('sqlblog'),
('sqlserver'),('maomao365.com')

--- merge into achieved if present, is updated
--- if not, deleting
the MERGE the INTO [maomao365.com] A
the USING (the SELECT keyId_B the AS 2, 'OTHER' info_B the AS) B
the ON (a.keyId = B. keyId_B)

WHEN MATCHED THEN
UPDATE SET a.info= b.info_B --更新

WHEN NOT MATCHED THEN
INSERT (info) VALUES(b.info_B); ---插入

go
select * from [maomao365.com]
go
MERGE INTO [maomao365.com] a
USING (SELECT 20 AS keyId_B, 'new Info' AS info_B ) b
ON ( a.keyId = b.keyId_B)

WHEN MATCHED THEN
UPDATE SET a.info= b.info_B --更新

WHEN NOT MATCHED THEN
INSERT (info) VALUES(b.info_B); ---插入
go

select * from [maomao365.com]
go
truncate table [maomao365.com]
drop table [maomao365.com]

Guess you like

Origin blog.51cto.com/14549989/2439768