msserver的update or insert语句

Posted 81

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了msserver的update or insert语句相关的知识,希望对你有一定的参考价值。

方案1:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; IF EXISTS (SELECT 1 FROM dbo.table WHERE PK = @PK) BEGIN UPDATE ... END ELSE BEGIN INSERT ... END COMMIT TRANSACTION;

方案2:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
UPDATE dbo.table SET ... WHERE PK = @PK;
IF @@ROWCOUNT = 0
BEGIN
  INSERT dbo.table(PK, ...) SELECT @PK, ...;
END
COMMIT TRANSACTION;
 

以上是关于msserver的update or insert语句的主要内容,如果未能解决你的问题,请参考以下文章

如何手动实现Try Insert和Insert Or Update

mysql insert or update

sql注入之 update/insert/delete篇

mybatis+postgresql insert, update or delete returning *问题

insert时报Cannot add or update a child row: a foreign key constraint fails (`yanchangzichan`.`products

update和insert + delete哪个更快?