473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Table Lock

Which lock type or isolation level should I use to be sure that no one
will read or write or do anything with the table I'm using?
Code block should look something like this:

lock table

read value from table
change value to new_value
update table set value = new_value

release lock
While I'm changing the value absolutly no one should be able to read
from the table.
Jul 23 '05 #1
7 22454
I think you want something like this:

BEGIN TRAN
DECLARE @value int
SELECT @value=YourColumn FROM TheTable WITH (TABLOCKX)
SET @value=...
UPDATE TheTable SET YourColumn=@value
COMMIT

But you should consider performing the modification directly in the
UPDATE statement.

Razvan

Jul 23 '05 #2
Razvan Socol wrote:
I think you want something like this:

BEGIN TRAN
DECLARE @value int
SELECT @value=YourColumn FROM TheTable WITH (TABLOCKX)
SET @value=...
UPDATE TheTable SET YourColumn=@value
COMMIT

But you should consider performing the modification directly in the
UPDATE statement.

Razvan


Might be... but I'm not sure that no one can read data from table
between SELECT and UPDATE. If you set isolation to dirty read than
you're able to read uncommited data, right?
Does "WITH (TABLOCKX)" lock the table until the end of transaction?
Jul 23 '05 #3
> Does "WITH (TABLOCKX)" lock the table until the end of transaction?

Yes
I'm not sure that no one can read data from table
between SELECT and UPDATE. If you set isolation to dirty read than
you're able to read uncommited data, right?


You are right, if someone uses "SET TRANSACTION ISOLATION LEVEL READ
UNCOMMITTED" or "WITH (NOLOCK)", then the uncommitted data may be read,
regardless of the TABLOCKX hint. You cannot guard against such a case
(except placing the entire database in SINGLE_USER mode).

Razvan

Jul 23 '05 #4
Jo¨ko ¦ugar (josko_bla@netgen_rem0ve_this_and_bla.hr) writes:
Which lock type or isolation level should I use to be sure that no one
will read or write or do anything with the table I'm using?
Code block should look something like this:

lock table

read value from table
change value to new_value
update table set value = new_value

release lock
While I'm changing the value absolutly no one should be able to read
from the table.


Razvan has already answered how you could do it. Now, next question is:
do you really want to do this? I have no idea what sort of application
you are working with, but generally you want as much concurrency in a
database system. Why lock the whole table, if you are changing only
one row? Not that I know if you are only changing one row, but I would
like you to think twice.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5
Erland Sommarskog wrote:

Razvan has already answered how you could do it. Now, next question is:
do you really want to do this? I have no idea what sort of application
you are working with, but generally you want as much concurrency in a
database system. Why lock the whole table, if you are changing only
one row? Not that I know if you are only changing one row, but I would
like you to think twice.


You are right. It's about only one row. Is it possible to do something like:

lock row in table

read value from table
change value to new_value
update table set value = new_value

release lock
Or... can I do it in one statement?
This is wrong, but I hope you understand what I'm trying to do:
update table set value = (select @n = value from table) + 1

I need to read the value, then save it in variable and then increment it
and update.

Jul 23 '05 #6
AK
why don't you begin transaction, update, and then select?

Jul 23 '05 #7
AK wrote:
why don't you begin transaction, update, and then select?


Ok, I will :)
Hope it does the trick. Thanks!

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: M Wells | last post by:
Hi All, I have a database that is serving a web site with reasonably high traffiic. We're getting errors at certain points where processes are being locked. In particular, one of our people...
17
by: Dr NoName | last post by:
Help! I have a table that multiple processes must be able to write to concurrently. However, it for some reason gets locked in exclusive mode. I narrowed it down to one SQL statement + some...
11
by: EoRaptor | last post by:
I`m exporting data from many Lotus Notes databases to a DB2 database using LotusScript. The LotusScript agents commit after EACH update/insert. Nevertheless, I keep getting transaction rollbacks on...
8
by: Roy Padgett | last post by:
I have a combo box where users select the customer name and can either go to the customer's info or open a list of the customer's orders. The RowSource for the combo box was a simple pass-through...
2
by: John Bokma | last post by:
I am quite new to MS SQL, and I want to read rows from a todo table, and when a row has been processed, I want to delete that row. Ages ago in MySQL I would probably have locked the table,...
2
by: adri4n | last post by:
as wat ive mentioned in the title.. im would like to know whether the a particular record/table is being locked in my program. some of the methods which i would like to develop are as below: ...
22
by: RayPower | last post by:
I'm having problem with using DAO recordset to append record into a table and subsequent code to update other tables in a transaction. The MDB is Access 2000 with the latest service pack of JET 4....
0
by: sang | last post by:
Hi I want to lock the table with both read and write there is no action held after lock the table. I know how to lock the table with read and write, lock table mytable read; lock table...
5
by: Roger | last post by:
I am doing some mass update on DB2 8.2 FP10 (say 400 to 500 rows on a 5000 row table) with just one commit. So this is taking lot of exclusive row locks as expected. When I run a select on the...
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.