我可以使用 ROWLOCK、UPDLOCK 锁定连接 SQL 语句中的记录吗?

发布于 2024-08-27 20:56:22 字数 776 浏览 6 评论 0原文

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

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 =

当我在存储过程中使用 join 命令从几个表中选择表时,是否可以对表 INVH 发出锁定?

任何建议,请!

I have a stored procedure to get the data I want:

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

After I get those records into cursor, I used ROWLOCK, UPDLOCK to lock all the related invoice numbers.

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

Can I issue locking on the table INVH at the point I select the table from a few table using join command at my store procedure?

Any advice, please!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国产ˉ祖宗 2024-09-03 20:56:22

是的,可以,只要您正确设置 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)”子句时,它就会起作用。

因此,您可以根据您的问题将锁放在您想要的位置。

Yes, you can, provided you format your SQL correctly. The general pattern you require is as follows:

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

Where {} is optional (omit the {}). So lock as you put the "WITH (UPDLOCK, ROWLOCK)" clause on each table you wish to lock, it will work.

So you can put the lock where you want it, as per your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文