如果记录存在则插入并更新,否则竞争条件
if exists (select itemcode from item where itemcode=1120)
update item
set itemname = 'laptop'
where itemcode = 1120
else
insert into item (itemcode,itemname)
values (1120,'laptop')
它将被多个用户使用。这个查询会给出竞争条件吗? 如果是,那么如何?我应该用什么来代替这个查询?
if exists (select itemcode from item where itemcode=1120)
update item
set itemname = 'laptop'
where itemcode = 1120
else
insert into item (itemcode,itemname)
values (1120,'laptop')
it will be used by multiple users. will this query give a race condition.
if yes, then how?what shall i use in place of this query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为此使用
交易
。确保在单个事务中锁定所有所需的表,然后释放它们。如果您有多个交易,您还可以为您的交易指定名称。
You may use
transaction
for this. Make sure to lock all your required tables in single transaction then release them.You can also give name to your transaction if you have multiple.