Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 15th, 2004, 10:49 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default i need to lock a row in table

hi,
all i need is to lock a row in a table for a while untile this record is updated.
 i am working under web environment that the user read the data then he update it with new entered data, between the two actions i need to lock that row ...
is that possible.


thanks

Ahmed Ali
Software Developer
__________________
Ahmed Ali
Senior Software Developer
 
Old May 15th, 2004, 06:16 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

By default SQL does the locking for you. There are of course different lock levels and performance issues you make want to consider

I am curious as to why would you want the user of your application to lock a particular row, in the event the user opens the record and does not updated that will escalated other locks; page, table and db locks if others have to update information within that table.

But anyhow books on line has information on row level lock works. and this article gives good information on how to accomplish your task.

http://www.sql-server-performance.co...ocks_hints.asp
 
Old May 15th, 2004, 11:35 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

what i mean about locking is to lock certain row for a while after read of data by a webform untill the update happen to this row i will then release the lock... this could be done if i made a new flag field to the table but i am looking for a SQl solution .
is SQL support that

Ahmed Ali
Software Developer
 
Old May 15th, 2004, 11:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

I think u r in wrong way!
Why do u wanna lock the record for a while?! First of all it waste many resources, Secondly not a recomended way.
If u wanna make a exclusive update on table do it in last moment, not all the time u show the record!
Another way is: store a variable like record's unique Number & control if its free to Edit/Show or not?!

HTH.

Always:),
Hovik Melkomian.
 
Old May 16th, 2004, 09:44 AM
Registered User
 
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aousterhout
Default

 I've never tried this, but does

SELECT au_lname FROM authors where (RecID = 123) WITH (HOLDLOCK)

and

SELECT au_lname FROM authors where (RecID = 123) WITH (NOLOCK)

work? If is does, how would you:
1. Manage when the User walks away without signing off
2. Look up the User's name who currently has control of the record

Andy

 
Old May 17th, 2004, 09:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

He's trying to prevent a race condition.. He doesn't want two people to open the same record and modify it.. Thereby overwriting the other changes.

EG:

User A opens
User B opens
User B Saves
User A Saves (User B's changes are overwritten)


It is probably best to have a "checkout" marker on the row that lists who has it checked out. And when someone else goes in to edit the row they are told it's already checked out by so and so and it's read only.



Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old May 17th, 2004, 09:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

P.S. - You should also use a timestamp and timeout the "lock" after a short time... or you end up with a problem of people leaving sessions without updating.. leaving the row's locked


Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee





Similar Threads
Thread Thread Starter Forum Replies Last Post
add new row to a table stealthdevil Access VBA 1 June 1st, 2006 11:04 AM
UPDATING 1 row with another row in same table rit01 SQL Server 2000 3 February 19th, 2006 08:55 AM
lock/unlock row in GridView jung1975 ASP.NET 2.0 Basics 0 January 4th, 2006 06:42 PM
first row of a table Adam H-W SQL Language 2 January 11th, 2005 10:15 AM
How to duplicate 1 row in table minhpx SQL Language 1 December 20th, 2004 09:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.