在事务SQL Server中选择语句

时间:2015-05-24 07:01:13

标签: sql sql-server

我有一个包含数千个代码的过程,包括insert,update,delete和select语句。在追踪一些选择语句后我发现了一个问题。没有交易的一个选择语句很好并且在2分钟后返回但是在交易内部在10分钟或更高时间后没有返回。请考虑我在select语句中使用nolock提示。

INSERT  #BoyInv ( Goods, Q )
    SELECT  DepotD.Goods,
            SUM(DepotD.Inventory * ( ObjectBehavior.BEDSide
                                     - ObjectBehavior.BESSide )) AS BoyInv
    FROM    DepotD WITH ( NOLOCK )
            INNER JOIN DepotH WITH ( NOLOCK ) ON DepotD.HID = DepotH.ID
            INNER JOIN DepotSerial WITH ( NOLOCK ) ON DepotH.Serial = DepotSerial.ID
            INNER JOIN Depot WITH ( NOLOCK ) ON DepotSerial.Depot = Depot.Code
            INNER JOIN ObjectBehavior WITH ( NOLOCK ) ON DepotSerial.Type = ObjectBehavior.Type
            INNER JOIN @tbl_Depot AS MyDepot ON Depot.Code = MyDepot.Depot
            INNER JOIN ( SELECT Type, PricingType
                         FROM  #DepotType
                       ) DepotType ON DepotSerial.Type = DepotType.Type
            INNER JOIN #Goods ON DepotD.Goods = #Goods.Goods
    WHERE   ( ObjectBehavior.Object = 18 )
            AND ( DepotH.[Date] <= @OldDate )
            AND ( Depot.CanPrice = 1 )
    GROUP BY DepotD.Goods

2 个答案:

答案 0 :(得分:1)

BEGIN TRANSACTION [Transaction]
BEGIN TRY
-- INSERT ALL YOUR SELECT, UPDATE, INSERT STATEMENTS HERE--

COMMIT TRANSACTION [Transaction]
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION [Transaction]
END CATCH

答案 1 :(得分:0)

而不是使用“与nolock” 以这种方式使用

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
GO
BEGIN TRANSACTION;
.
.
COMMIT TRANSACTION;
SET TRANSACTION ISOLATION LEVEL READ COMMITTED