1

我有一个存储过程来获取我想要的数据:

SELECT  a.SONum, a.Seq1, a.SptNum, a.Qty1, a.SalUniPriP, a.PayNum, a.InvNum, 
    a.BLNum, c.ETD, c.ShpNum,  f.IssBan
FROM OrdD a JOIN OrdH b ON a.SONum = b.SONum  
    LEFT JOIN Invh c ON a.InvNum = c.InvNum 
    LEFT JOIN cus d ON b.CusCod = d.CusCod  
    LEFT JOIN BL e ON a.BLNum = e.BLNum
    LEFT JOIN PayMasH f ON f.PayNum = a.PayNum
    LEFT JOIN Shipment g ON g.ShpNum = c.ShpNum  
WHERE b.CusCod IN (SELECT CusCod FROM UsrInc WHERE UseID=@UserID and UseLev=@UserLvl) 
    AND d.CusGrp = @CusGrp

将这些记录放入光标后,我曾经ROWLOCK, UPDLOCK锁定所有相关的发票编号。

SELECT InvNum FROM Invh WITH (ROWLOCK,UPDLOCK) WHERE InvNum =

我可以INVH在存储过程中使用 join 命令从几个表中选择表时锁定表吗?

任何建议,请!

4

1 回答 1

1

是的,你可以,只要你正确地格式化你的 SQL。您需要的一般模式如下:

BEGIN TRAN

SELECT    *
FROM      table_1 AS t1 {WITH (UPDLOCK, ROWLOCK)}
JOIN      table_2 AS t2 {WITH (UPDLOCK, ROWLOCK)}
ON        t1.a = t2.a

COMMIT TRAN   -- locking ends here

其中{}是可选的(省略{})。因此,当您将“ WITH (UPDLOCK, ROWLOCK)”子句放在要锁定的每个表上时进行锁定,它将起作用。

所以你可以根据你的问题把锁放在你想要的地方。

于 2011-01-05T20:59:45.200 回答