On Mon, 1 Sep 2003 20:52:21 +0300, "Loukas Marinis"
[quoted text, click to view] <ca6lma@Hotmail.com> wrote:
>from vb6 using unbound forms i call a stored procedures which include just a
>select statement like
>
>select * from users where userId = @userId
>
>Always return one row.
>
>My question is how i can determine if this record is locked regardsless of
>whether it will be changed or not; just to avoid complications
>
>I want to use this in the case another user want to edit the same record and
>hence running the same procedure.
Oh, what you asked!
The recommended approach to most modern database usage is "optimistic"
locking, in which you do something like taking the timestamp from the
record you read and use it if and when you decide to do an update.
The server can then reject your update, saying "someone beat you to
it." This is acceptable if it happens rarely enough, and in many
cases, that is the statistical situation.
So, generally, records are not detectably locked at all!
Now, if you're an old fart like myself and still prefer to do what
they call "pessimistic" locking now and again, SQLServer does support
this, but the only way I know of detecting it, is to attempt to lock
it yourself. If your lock is issued, you got it, and if it is
rejected, someone else does. There are good and bad ways to go about
this all, of course. You could attempt the lock and release it if you
get it, but ... well, you see the tangles that result. To attempt
such a lock, do a begin trans, then do a select "with (updlock)".
Since even SQLServer actually does lock records at some point,
especially in big, fat transactions, your update detection may indeed
detect some locked records, even if they are only implicitly locked
for very short periods in most cases. Though, in general, you seldom
need to or want to know about such things.
See BOL for more details on stuff like updlock.
Best of luck,
J.