我正在嘗試編寫一個存儲過程,該過程讀取表的特定行中的列,然後使用新值更新該列。原始。返回。從行中選擇一列並將該列更新爲另一個值
我希望它鎖定其他人的行,直到我完成。過程是什麼?
我有類似
CREATE PROCEDURE [dbo].[aptc_Prt_NextDocumentNumberGet]
(@_iFormatConfigID INT, @_oNextDocumentNumber FLOAT OUTPUT)
AS
BEGIN
DECLARE @FrameworkConfig XML
SET @_oNextDocumentNumber = - 1
DECLARE @NewNextDocumentID FLOAT
SELECT
@_oNextDocumentNumber = FrameworkConfig.value('(/Parameters/Parameter[@Name="NextDocNo.NextDocumentNumber"])[1]', 'float')
FROM
[ttcPrtFormatConfig] WITH (ROWLOCK)
WHERE
FormatConfigID = @_iFormatConfigID
-- Select the Next Doc num out of the xml field
-- increment appropriate control and set output
IF @_iFormatConfigID IS NOT NULL
BEGIN
-- set what will be the "next" doc number after we add this current txn
IF (ABS(@_oNextDocumentNumber - 99999999999999999) < 0.0001)
BEGIN
SELECT @NewNextDocumentID = 1
END
ELSE
BEGIN
SELECT @NewNextDocumentID = @_oNextDocumentNumber + 1
END
UPDATE [ttcPrtFormatConfig]
WITH (ROWLOCK)
SET FrameworkConfig.modify('
replace value of
(/Parameters/Parameter[@Name="NextDocNo.NextDocumentNumber"]/text())[1]
with sql:variable("@NewNextDocumentID")')
WHERE FormatConfigID = @_iFormatConfigID
END
END
您只需要一個事務和UPDLOCK HOLDLOCK。看到我的回答 –