473,661 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you lock an entire table?


Hi,
I need to lock a table so that Inserts are prevented as well as deleted and
updates. At present I'm thinking this might do it:

SELECT * FROM myTable WITH(UPLOCK)

but then again I'm not sure whether this will cover the insert case.

Thanks,

Robin
Jul 23 '05 #1
5 2950
Robin Tucker (id************ *************@r eallyidont.com) writes:
I need to lock a table so that Inserts are prevented as well as deleted
and updates. At present I'm thinking this might do it:

SELECT * FROM myTable WITH(UPLOCK)

but then again I'm not sure whether this will cover the insert case.


To cover inserts, you need the serializable isolation level, and as far
as I know, that is what UPDLOCK buys you. Testing is always a good idea,
though.

It would be interesting to know why you think you need to lock the entire
table. It may be the right thing to do, but it sounds a little funny.

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

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

Yes, it is funny indeed. The basic situation is that I was spec'd to write
a "single user/client" system, installing MSDE on the client PC along with
the client software. All of a sudden "multi-user" is a basic product
requirement, although I didn't write a core suitable for concurrency (this
is kindof a real-time system, so concurrency really needed to be designed in
from the start. So, as a stop-gap measure, I am wanting to lock a table,
write a "I'm using this now" record, unlocking it when the user times out or
logs off. Okay, this is dumbass I know, but it will take 4 months to
rewrite the core to use the correct design pattern for this new
requirement - this is a quick "hack" to get things up and running pending
the new core ;)
"Erland Sommarskog" <es****@sommars kog.se> wrote in message
news:Xn******** **************@ 127.0.0.1...
Robin Tucker (id************ *************@r eallyidont.com) writes:
I need to lock a table so that Inserts are prevented as well as deleted
and updates. At present I'm thinking this might do it:

SELECT * FROM myTable WITH(UPLOCK)

but then again I'm not sure whether this will cover the insert case.


To cover inserts, you need the serializable isolation level, and as far
as I know, that is what UPDLOCK buys you. Testing is always a good idea,
though.

It would be interesting to know why you think you need to lock the entire
table. It may be the right thing to do, but it sounds a little funny.

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 23 '05 #3
Robin Tucker (id************ *************@r eallyidont.com) writes:
Yes, it is funny indeed. The basic situation is that I was spec'd to
write a "single user/client" system, installing MSDE on the client PC
along with the client software. All of a sudden "multi-user" is a basic
product requirement, although I didn't write a core suitable for
concurrency (this is kindof a real-time system, so concurrency really
needed to be designed in from the start. So, as a stop-gap measure, I
am wanting to lock a table, write a "I'm using this now" record,
unlocking it when the user times out or logs off. Okay, this is dumbass
I know, but it will take 4 months to rewrite the core to use the correct
design pattern for this new requirement - this is a quick "hack" to get
things up and running pending the new core ;)


If the table is only there to act as a locking mechanism, I strongly
recomend you to look at application locks instead. Look up
sp_getapplock and sp_releaseapplo ck in Books Online.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
You know what? We solved this problem without having to write any SQL code
at all. We are now using USB dongle with a one license per server hard
limit on it. Just a bit of code in the client required. ;)

Now....... I'm working on that multi-user core.

"Erland Sommarskog" <es****@sommars kog.se> wrote in message
news:Xn******** **************@ 127.0.0.1...
Robin Tucker (id************ *************@r eallyidont.com) writes:
Yes, it is funny indeed. The basic situation is that I was spec'd to
write a "single user/client" system, installing MSDE on the client PC
along with the client software. All of a sudden "multi-user" is a basic
product requirement, although I didn't write a core suitable for
concurrency (this is kindof a real-time system, so concurrency really
needed to be designed in from the start. So, as a stop-gap measure, I
am wanting to lock a table, write a "I'm using this now" record,
unlocking it when the user times out or logs off. Okay, this is dumbass
I know, but it will take 4 months to rewrite the core to use the correct
design pattern for this new requirement - this is a quick "hack" to get
things up and running pending the new core ;)


If the table is only there to act as a locking mechanism, I strongly
recomend you to look at application locks instead. Look up
sp_getapplock and sp_releaseapplo ck in Books Online.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 23 '05 #5
Robin Tucker (id************ *************@r eallyidont.com) writes:
You know what? We solved this problem without having to write any SQL
code at all. We are now using USB dongle with a one license per server
hard limit on it. Just a bit of code in the client required. ;)


Ah, creative solutions! I like that!

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

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

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

Similar topics

4
20927
by: Richard Holliingsworth | last post by:
Hello: I have an Access 2K form I built from a SQL Server 7.0 view. I want to lock certain fields in the database from users so they can see them on the views and forms, but NOT be able to edit them. I've looked in BOL, MS SQL Server web page and SQL Server 7.0 books and could not find how to do this. Any advise will be greatly appreciated.
3
5621
by: rkusenet | last post by:
Hi, Version Info: SQLSERVER 2000 SP3 I am trying to understand how SQL Server works with SERIALIZABLE read. I am fairly new to SQL Server, having mainly worked with Informix. We are getting lock timeout error in our application. Our lock timeout is set to 20 seconds. I am trying to investigate why locks are held for such a long time in the first place.
2
4465
by: xixi | last post by:
i am using type 4 driver with fp3 on windows 64bit server, auto commit is true, cursor is CS type. i have a sensitive , updatable cursor open and bring back a one row resultset, i get this locklist Lock Name = 0x020027011C0000000000000052 Lock Attributes = 0x00000000 Release Flags = 0x00000002 Lock Count = 1
5
7948
by: xixi | last post by:
hi, i am using db2 udb v8.1 on windows, i create a index on a unique value column on a table to try to create row lock, here is what i do , sql = select value from table where id=1 for update, the auto commit is true. if the table has more than one row, i got row U lock, but if the table has only one row , i get row X lock, is this right, why is that? i would like this only create row U lock, not row X lock. thanks for advice
0
3377
by: Bruce Pullen | last post by:
DB2 v7.2 (FP7 - DB2 v7.1.0.68) on AIX 5.2.0.0. We're seeing unexpected single row (then commit) insert locking behaviour. We're seeing Applications that already hold row-level W locks in lock-wait, waiting to acquire row-level X locks. The lock-waits are behind applications that have row-level X locks on different rows (honestly). Both executing and lock-waiting applications have been granted IX table locks.
8
6801
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 query: SELECT DISTINCT , , ,City, Region FROM Customers ORDER BY Customers.; This was working fine until a couple of weeks ago. Now whenever someone has the form open, this statement locks the entire Customers table.
5
29581
by: Seeker | last post by:
Hello, I've read conflicting posts about . Does it or does it not lock the entire object? In my simple test it appears to block just the method but I wouldn't exactly call my meager test conclusive... thanks, Scott
9
15523
by: kavallin | last post by:
I receives the following in the db2diag.log file many times / day : 2007-03-05-14.55.24.836553+060 E12415C457 LEVEL: Warning PID : 2785 TID : 1 PROC : db2agent (dbname) INSTANCE: db2inst1 NODE : 000 DB : dbname APPHDL : 0-946 APPID: *LOCAL.db2inst1.070305135434 FUNCTION: DB2 UDB, data management, sqldEscalateLocks, probe:3 MESSAGE : ADM5502W The escalation of...
6
7711
by: michael.spoden | last post by:
Hi, how can I fix lock-waits during an online backup? Is an online backup in DB2 V8.2 not realy online? I'm using DB2 V8.2 Fixpak 15 on Linux. The command to perform the backup is: db2 backup db BPEDB online to / var/tmp During the backup the application hangs on just one table
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8758
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6185
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.