我在 SQL Server 中有一个存储过程。当我从 Delphi 执行它时,我收到一条死锁消息!
事务(进程 ID 60)在锁上死锁 | 具有另一个进程的通用等待对象资源,并已被选为死锁牺牲品
为了解决这种情况,我应该在下面的查询中进行哪些更改。我的存储过程没有参数。
ALTER PROCEDURE [dbo].[RepairStocks]
AS
BEGIN
SET NOCOUNT ON;
drop table [dbo].[stocksss]
select
Barkod,
sum(kolicina) as Kolicina,
Max(Kategorija) as Kategorija,
Max(Artikal) as Artikal,
Max(Opis) as Opis,
Max(N_cena) as N_cena,
Max(N_cena) * sum(kolicina) as N_Iznos,
Max(P_cena) as P_cena,
Max(P_Cena) * sum(kolicina) as P_Iznos,
Max(datum) as datum,
Max(Golemina) as Golemina
into [dbo].[stocksss]
from [dbo].[Stocks]
group by Barkod
drop table [dbo].[Stocks];
select *
into [dbo].[Stocks]
from [dbo].[stocksss]
END
任何帮助深表感谢...
编辑我的查询后
下面的查询适用于 SQL Server,但是当我从 Delphi 执行此操作时,我再次收到死锁消息。这是我的查询的样子:
ALTER PROCEDURE [dbo].[RepairStocks]
AS
BEGIN
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
select
Barkod,
sum(kolicina) as Kolicina,
Max(Kategorija) as Kategorija,
Max(Artikal) as Artikal,
Max(Opis) as Opis,
Max(N_cena) as N_cena,
Max(N_cena) * sum(kolicina) as N_Iznos,
Max(P_cena) as P_cena,
Max(P_Cena) * sum(kolicina) as P_Iznos,
Max(datum) as datum,
Max(Golemina) as Golemina
into #tmp_stocks
from [dbo].[Stocks]
group by Barkod
--------------------------------
;
DROP TABLE [dbo].[Stocks]
;
--------------------------------
select *
into [dbo].[Stocks]
from #tmp_stocks with (nolock)
;
drop table #tmp_stocks
;
COMMIT TRAN
END
在Delphi中,我使用以下代码执行命令:
RepairStocks.Close;
RepairStocks.SQL.Clear;
RepairStocks.SQL.Add('EXEC [dbo].[RepairStocks]');
RepairStocks.ExecSQL;