Upsert导致死锁

时间:2015-06-04 09:47:13

标签: sql-server-2012

我有以下存储过程:

    CREATE PROCEDURE [dbo].[upsert2] (
    @ID varchar(20),
    @Status varchar(50),
    @Price  varchar(20),
    @Volume varchar(20),
    @Currency   varchar(20),
    @InstrumentID   varchar(20),
    @SeqID  varchar(20),
    @EntryDate  datetime)
AS
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
    BEGIN TRANSACTION
    MERGE INTO Trades
USING 
    (SELECT @ID AS 'ID',@Status AS 'Status',@Price AS 'Price',@Volume AS 'Volume',@Currency AS 'Currency',@InstrumentID AS 'InstrumentID',@SeqID AS 'SeqID',@EntryDate AS 'EntryDate') AS Query
    ON Trades.ID = Query.ID AND Trades.InstrumentID = Query.InstrumentID AND Trades.SeqID = Query.SeqID
WHEN MATCHED THEN
    UPDATE SET
        Trades.Status = Query.Status,
        Trades.Price = Query.Price,
        Trades.Volume = Query.Volume,
        Trades.Span = Query.Span,
        Trades.Currency = Query.Currency,
        Trades.InstrumentID = Query.InstrumentID,
        Trades.SeqID = Query.SeqID,
        Trades.EntryDate = Query.EntryDate

WHEN NOT MATCHED THEN
        INSERT (Status,Price,Volume,Span,Currency,InstrumentID,SeqID,EntryDate) 
        VALUES (Query.Status,Query.Price,Query.Volume,Query.Span,Query.Currency,Query.InstrumentID,Query.SeqID,Query.EntryDate);
COMMIT

这个过程由4个不同的进程调用,这些进程同时运行,导致它产生死锁并使一些调用失败,我可以修复的程序有什么问题来解决死锁,因为我没有更多想法如何解决它。

0 个答案:

没有答案