作者vi000246 (Vi)
看板Database
标题[SQL ] 资料库update时发生死结
时间Thu Sep 7 17:28:28 2017
(针对
SQL 语言的问题,用这个标题。请用 Ctrl+Y 砍掉这行)
资料库名称: SQL Server
资料库版本:Microsoft SQL Server 2012 - 11.0.5343.0
内容/问题描述:
我的程式会依序执行以下SQL
--判断指定AID的ItemID是否为空
select * from testdb
where AID = @AID and PokeDate = @date and isnull(ItemID, 0)>0
--如果资料不存在就存档
BEGIN
IF NOT EXISTS (SELECT * FROM testdb
WHERE AID = @AID and PokeDate = @date)
BEGIN
Insert into (Acc,AID,CreateTime,UserIP,PokeDate)
values(@Acc,@AID,@CreateTime,@UserIP,@PokeDate)
END
END
--Update刚才Insert进来的资料
update testdb set
ItemID = @ItemID,
ItemCount = @ItemCount,
ItemName = @ItemName,
RewardQueueID = @RewardQueueID
where Acc = @Acc and PokeDate =
CONVERT(char(10), @PokeDate ,126)
死结会发生在update的query
完整错误讯息
Transaction (Process ID 74) was deadlocked on lock |
communication buffer resources with another process
and has been chosen as the deadlock victim.
Rerun the transaction.
我试过在select加上with(nolock)也没用
这情形之前没发生过 怀疑是Insert那段的IF判断导致的
我直接在DB尝试触发死结 设了延迟但都正常
不太清楚是哪段SQL导致资源被lock住
想问这种情形该怎麽debug呢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.218.40.109
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1504776511.A.6A4.html
1F:推 elfishhare: 会不会是 insert 未 commit 又被 update 的关系? 10/20 00:46
2F:推 jackiechin: 是insert的原因 11/16 19:14