如何在SQL存储过程中使用读取锁

时间:2018-09-20 10:13:27

标签: sql sql-server tsql

我的主要过程是spPet_TransferPettyCashToGL。从此过程中,我将调用(fnApp_GetCurrentBatchNo)函数并获取我的批号,并在过程中使用该过程。在退出程序之前,我要更新批号。

我想锁定我的batch no表,并且在更新批处理之后不希望发布此表。

主要SP:

ALTER PROCEDURE [dbo].[spPet_TransferPettyCashToGL] 
@ComCod as varchar(2),
@LocCod as varchar(2),
@PetDat as varchar(8)
AS
BEGIN
    /* Get New Batch No From Function */
    DECLARE @CurrentBatchNo as int
    EXECUTE @CurrentBatchNo = Accounts.dbo.fnApp_GetCurrentBatchNo @ComCode = @ComCod,
                                                               @LocCode = @LocCod

    /* Due to large code I ignored unnecessary areas. */

    /* UPDATE Provisional_Posting_Log  {Last Update Batch NO} */
    EXECUTE spApp_UpdateProvisionalPostingLogBatchNo @ComCod,
                                                     @LocCod,
                                                     @CurrentBatchNo
END

给出Batch No的函数:

ALTER FUNCTION [dbo].[fnApp_GetCurrentBatchNo] 
(
    @ComCode varchar(2),
    @LocCode varchar(2)
)
RETURNS int
AS
BEGIN
    DECLARE @CurrentBatchNo int

    SELECT @CurrentBatchNo = (SELECT (Last_Updated_Batch_No + 1)
                                 FROM Provisional_Posting_Log
                                      WHERE Company_Code = @ComCode AND
                                            Location_Code = @LocCode)

    RETURN @CurrentBatchNo
END

SP将batch no更新为从上述FN获得的值:

ALTER PROCEDURE [dbo].[spApp_UpdateProvisionalPostingLogBatchNo]
    @ComCod as varchar(2),
    @LocCod as varchar(2),
    @CurrentBatchNo as int

AS
BEGIN
    UPDATE Provisional_Posting_Log
           SET Last_Updated_Batch_No = CAST(@CurrentBatchNo as decimal(24,0))
               WHERE Company_Code = @ComCod AND
                     Location_Code = @LocCod
END

在获得批号时如何锁定Provisional_Posting_Log表?如何在更新批号后将其释放?

0 个答案:

没有答案