SQL Server 2014死锁问题

时间:2016-12-15 09:38:30

标签: sql-server reporting-services deadlock

CREATE PROCEDURE [dbo].[TraceabilityReport]
    @StartDate date,
    @EndDate date
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
        pro.Reference, pro.BatchNumberID,
        inm.Name as 'Product Name',
        inm.code as 'Product Code',
        st.TransactionQTY, st.TransactionWeight,
        (SELECT TOP 1 b.Name 
         FROM APGoodsReceipt a 
         INNER JOIN APGoodsReceiptDetail ap ON a.APGoodsReceiptID = ap.APGoodsReceiptID
         INNER JOIN BPMaster b ON b.BPMasterID = a.BPMasterID_Supplier 
         WHERE ap.BatchNumberID = pro.BatchNumberID 
           AND ap.Deleted IS NULL 
           AND a.Deleted IS NULL) AS 'Supplier',
        (SELECT SUM(TransactionQTY) 
         FROM StockTransaction 
         WHERE StockTransaction.MasterTableID = pro.PROrderID 
           AND deleted IS NULL 
           AND StockTransaction.IsBox IS NULL) AS 'IntoSlicing',
        (SELECT SUM(TransactionWeight) 
         FROM StockTransaction 
         WHERE StockTransaction.MasterTableID = pro.PROrderID 
           AND deleted IS NULL 
           AND StockTransaction.IsBox IS NULL) AS 'WeightIntoSlicing',
        (SELECT SUM(TransactionWeight) from StockTransaction where BatchNumberID = bn.BatchNumberID and deleted is null and StockTransaction.IsBox is null and WarehouseID = 2002) as 'WeightOut',

(SELECT SUM(TransactionQTY) from StockTransaction where  StockTransaction.MasterTableID = pro.PROrderID and deleted is null and StockTransaction.IsBox is null) + (SELECT ISNULL((SUM(s.TransactionQTY)),0)     
       FROM ARDispatch d
       INNER JOIN ARDispatchDetail dd
          ON d.ARDispatchID = dd.ARDispatchID
      INNER JOIN StockTransaction s
         ON s.MasterTableID = dd.ARDispatchDetailID
      INNER JOIN BatchNumber bn
         ON bn.BatchNumberID = s.BatchNumberID
        inner join RouteMaster rm
        on d.RouteID = rm.RouteID     
   WHERE bn.BatchNumberID = pro.BatchNumberID AND dd.Deleted IS NULL AND s.Deleted IS NULL AND S.ISBOX IS NULL AND s.NouTransactionTypeID = 7 AND (rm.Name = 'Joints')) as 'TotalJointsandIntoSlicing',


(SELECT ISNULL((SUM(s.TransactionWeight)),0)    
        FROM ARDispatch d
       INNER JOIN ARDispatchDetail dd
          ON d.ARDispatchID = dd.ARDispatchID
      INNER JOIN StockTransaction s
         ON s.MasterTableID = dd.ARDispatchDetailID
      INNER JOIN BatchNumber bn
         ON bn.BatchNumberID = s.BatchNumberID
        inner join RouteMaster rm
        on d.RouteID = rm.RouteID     
   WHERE bn.BatchNumberID = pro.BatchNumberID AND dd.Deleted IS NULL AND s.Deleted IS NULL AND S.ISBOX IS NULL AND s.NouTransactionTypeID = 7 AND (rm.Name = 'Joints')) as 'Joints Dispatch Weight',

   (SELECT ISNULL((SUM(s.TransactionWeight)) ,0)            

    FROM ARDispatch d
       INNER JOIN ARDispatchDetail dd
          ON d.ARDispatchID = dd.ARDispatchID
      INNER JOIN StockTransaction s
         ON s.MasterTableID = dd.ARDispatchDetailID
      INNER JOIN BatchNumber bn
         ON bn.BatchNumberID = s.BatchNumberID
        inner join RouteMaster rm
        on d.RouteID = rm.RouteID         
     WHERE bn.BatchNumberID = pro.BatchNumberID AND dd.Deleted IS NULL AND s.Deleted IS NULL AND S.ISBOX IS NULL AND s.NouTransactionTypeID = 7 AND (rm.Name = 'Slicing')) as 'Slicing Dispatched Weight',

         (SELECT ISNULL((SUM(s.TransactionQTY)) ,0)             

       FROM ARDispatch d
       INNER JOIN ARDispatchDetail dd
          ON d.ARDispatchID = dd.ARDispatchID
      INNER JOIN StockTransaction s
         ON s.MasterTableID = dd.ARDispatchDetailID
      INNER JOIN BatchNumber bn
         ON bn.BatchNumberID = s.BatchNumberID
        inner join RouteMaster rm
        on d.RouteID = rm.RouteID         
     WHERE bn.BatchNumberID = pro.BatchNumberID AND dd.Deleted IS NULL AND s.Deleted IS NULL AND S.ISBOX IS NULL AND s.NouTransactionTypeID = 7 AND (rm.name = 'Joints')) as 'Total Joints Out', 

     (SELECT ISNULL((SUM(s.TransactionQTY)) ,0)             

    FROM ARDispatch d
       INNER JOIN ARDispatchDetail dd
          ON d.ARDispatchID = dd.ARDispatchID
      INNER JOIN StockTransaction s
         ON s.MasterTableID = dd.ARDispatchDetailID
      INNER JOIN BatchNumber bn
         ON bn.BatchNumberID = s.BatchNumberID
        inner join RouteMaster rm
        on d.RouteID = rm.RouteID

     WHERE bn.BatchNumberID = pro.BatchNumberID AND dd.Deleted IS NULL 
     AND s.Deleted IS NULL AND S.ISBOX IS NULL AND s.NouTransactionTypeID = 7 
     AND (rm.Name = 'Slicing' or rm.name = 'Joints')) as 'Total Out',


    ((SELECT SUM(WeightReceived) from apGoodsReceiptdetail apg where apg.BatchNumberID = pro.BatchNumberID) /

    ((SELECT ISNULL((SUM(s.TransactionWeight)),0)   
        FROM ARDispatch d
       INNER JOIN ARDispatchDetail dd
          ON d.ARDispatchID = dd.ARDispatchID
      INNER JOIN StockTransaction s
         ON s.MasterTableID = dd.ARDispatchDetailID
      INNER JOIN BatchNumber bn
         ON bn.BatchNumberID = s.BatchNumberID
        inner join RouteMaster rm
        on d.RouteID = rm.RouteID     
   WHERE bn.BatchNumberID = pro.BatchNumberID AND dd.Deleted IS NULL 
   AND s.Deleted IS NULL AND S.ISBOX IS NULL AND s.NouTransactionTypeID = 7 AND (rm.Name = 'Joints')) 
   +
   (SELECT SUM(TransactionWeight) from StockTransaction 
   where  StockTransaction.MasterTableID = pro.PROrderID and deleted is null 
   and StockTransaction.IsBox is null))) as 'MC',

    ((SELECT ISNULL((count(s.TransactionQTY)),0)    
        FROM ARDispatch d
       INNER JOIN ARDispatchDetail dd
          ON d.ARDispatchID = dd.ARDispatchID
      INNER JOIN StockTransaction s
         ON s.MasterTableID = dd.ARDispatchDetailID
      INNER JOIN BatchNumber bn
         ON bn.BatchNumberID = s.BatchNumberID
        inner join RouteMaster rm
        on d.RouteID = rm.RouteID   
    inner join INMaster inm on 
         inm.INMasterID = s.INMasterID
    inner join INGroup ing on ing.INGroupID = inm.INGroupID    
   WHERE bn.BatchNumberID = pro.BatchNumberID AND dd.Deleted IS NULL 
   AND s.Deleted IS NULL AND S.ISBOX IS NULL AND s.NouTransactionTypeID = 7 AND (rm.Name = 'Joints') AND ing.Name like 'Honey Glazed%'))
   as 'Honey Glazed'



 from BatchNumber bn inner join PROrder pro on bn.BatchNumberID = pro.BatchNumberID
 inner join StockTransaction st on st.BatchNumberID = bn.BatchNumberID
 inner join INMaster inm on inm.INMasterID = st.INMasterID 




 where   st.NouTransactionTypeID = 2 and  pro.NouDocStatusID = 1 and 
 bn.Deleted is null and st.Deleted is null 
 and st.TransactionDate between @StartDate and @enddate
 order by st.TransactionDate
END

我有一个脚本,我正在用于ssrs报告。主要查询是带回一个批次表。对于每个批次,我需要分解并显示已发送的库存权重总和。 (每个库存项目与批次相关联,并且有多个库存项目关联)。

为了做到这一点,我正在使用一些子查询。由于报告每天只运行几次,因此查询效率不是主要问题。

但是,我运行查询时“大多数时间”出现SQL错误 -

  

事务(进程ID 60)在锁资源上与另一个进程发生死锁,并被选为死锁牺牲品。重新运行该交易。

有没有人对导致这种情况的原因以及如何解决这个问题有任何想法?

0 个答案:

没有答案