473,401 Members | 2,125 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,401 software developers and data experts.

Read, modify table (locking) question

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, select, process
a row, delete a row, unlock the table.

I have been reading through the documentation from MS SQL, but it's not
clear what exactly I should do.

Since I want to lock only one table in the select (the others just provide
data, and are not modified), what's a good solution?
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Feb 3 '06 #1
2 4398
John Bokma <jo**@castleamber.com> wrote:
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, select,
process a row, delete a row, unlock the table.

I have been reading through the documentation from MS SQL, but it's
not clear what exactly I should do.

Since I want to lock only one table in the select (the others just
provide data, and are not modified), what's a good solution?


what I came up with:

BEGIN TRANSACTION
SELECT TOP 10 ..... FROM A WITH(ROWLOCK,HOLDLOCK), B, C WHERE ....
.....
.... delete each row in A in TOP 10
END TRANSACTION

what I want to prevent is that more then one process selects 10 rows, and
starts to delete rows (from A) that are selected by any of the other ones.

to me, a rowlock is sufficient, and fine grained enough, and the hold lock
holds it to the end of transaction.

Am I right?

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Feb 3 '06 #2
John Bokma (jo**@castleamber.com) writes:
John Bokma <jo**@castleamber.com> wrote:
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, select,
process a row, delete a row, unlock the table.

I have been reading through the documentation from MS SQL, but it's
not clear what exactly I should do.

Since I want to lock only one table in the select (the others just
provide data, and are not modified), what's a good solution?


what I came up with:

BEGIN TRANSACTION
SELECT TOP 10 ..... FROM A WITH(ROWLOCK,HOLDLOCK), B, C WHERE ....
....
... delete each row in A in TOP 10
END TRANSACTION

what I want to prevent is that more then one process selects 10 rows, and
starts to delete rows (from A) that are selected by any of the other ones.

to me, a rowlock is sufficient, and fine grained enough, and the hold lock
holds it to the end of transaction.

Am I right?


Difficult to say with the small amount of information, but it does not
seem quite right.

The smallest change you should do is to is to remove ROWLOCK, but insert
UPDLOCK instead. ROWLOCK is sort of meaningless. Either you have a good
index to locate the rows, and you will get rowlocks. Or you don't have
good indexes, and SQL Server will have to lock the entire table.

UPDLOCK is a shared lock that does not block other readers, but it
blocks others that try to use UPDLOCK. With only HOLDLOCK and you have
two processes coming to the place at the same time, will lock the same
10 ten rows, and then when they to delete, they will block each other.

But there may be other things you could consider. It could be the case
that application locks are a better choice. An application lock is a
lock on a user-defined resource (that is a text string) which is handled
by the Lock Manager. But I know too little about your application to
tell whether it would fit it here.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 3 '06 #3

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

Similar topics

4
by: francis70 | last post by:
Hi, I have these 2 problem? Is there a way in Oracle to read UNCOMMITED data. i.e. in Oracle the normal behaviour is that a user's updates to a table are visible to other users ONLY when the...
29
by: pb648174 | last post by:
I have a very long transaction that runs on the same database that other users need to use for existing data. I don't care if they see data from the transaction before it is done and am only using...
6
by: laurenq uantrell | last post by:
Is it possible to use With (NOLOCK) and With (READPAST) in the same SELECT query and what whould be the syntax? @param int SELECT myRow FROM dbo.myTable WITH (NOLOCK) WHERE
5
by: chintalas | last post by:
Here when many users are trying to update the same table the dead lock situation is arising. I like to know how i can put a lock at record level in my table, so that the dead lock situation will...
1
by: Zri Man | last post by:
I have found the DB2/UDB implentation of locks a little more hard to deal with than Oracle's locking. First I realize there is lock escalation. But it would help me if somebody loudly thought...
3
by: Freelobly Li | last post by:
Hi all, I have encountered two problems when using linked tables; the linked table is connected to SQL server 2000. 1) How can I add the N prefix of a string constant in a query in order to...
9
by: rdemyan via AccessMonster.com | last post by:
How does one handle the situation where two different users are looking at bound forms that display data from a table. User #1 makes a change to the data, but User #2 is just perusing the data....
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....
5
by: troy | last post by:
Could someone please point me in the right direction on how to read in a large query with .net. I am trying to emulate a legacy database system so I don't know the upper bounds of the sql query....
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.