读取时锁定Sql Server表 [英] Lock a Sql Server Table While Read

查看:59
本文介绍了读取时锁定Sql Server表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用winform应用程序,在其中单击窗体上的按钮时,我必须先读取示例表值billNo和abd kotNo,然后递增这些值(在单个存储过程中).但是问题在于,任何其他用户也可以同时读取相同的值,并增加这些值.因此导致两个biil具有相同的Billno.

 创建  TABLE  [dbo].[示例](
    [billNo] [十进制]( 6  0 )  NULL ,
    [kotNo] [十进制]( 6  0 )  NULL ,
    [initials] [字符]( 3 )>不  NULL ,
    [unit_code] [字符]( 3 )>不  NULL 
)




请帮助我纠正我的问题.

问候
Vijay

解决方案

我认为此链接将为您提供帮助

点击我 [开始交易 [ UPDATE MyTable SET COL = COl + 1 位置 ...


但是,我不会将计算结果存储在表中,因此请考虑重新设计应用程序以在运行时获取您感兴趣的金额.这样可以避免多余的逻辑,并确保始终获得正确的结果.


I am working on a winform application where on a butoon click in a form, I have to first read the Sample table values billNo abd kotNo and then increment those values(in a single stored procedure). But the problem area is that any other user can also read the same value simultaneously, and increment those values. so resulting in two biils having same billno.

CREATE TABLE [dbo].[Sample](
    [billNo] [decimal](6, 0) NOT NULL,
    [kotNo] [decimal](6, 0) NOT NULL,
    [initials] [char](3) NOT NULL,
    [unit_code] [char](3) NOT NULL
) 




please help me to rectify my problem.

regards
Vijay

I think this link will help you

Click Me[^]


First of all you should have a transaction if not already present. See: BEGIN TRANSACTION[^]

Now if this is the same table you''re reading and updating, why not use a single update statement. Something like:

UPDATE MyTable
SET COL = COl + 1
WHERE ...


However, I would not store calculations in a table so consider redesigning the application to fetch the amount you''re interested in at runtime. This would prevent extra logic and ensure that you always get correct results.


这篇关于读取时锁定Sql Server表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆