Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Locks

Author  Topic 

jogin malathi
Posting Yak Master

117 Posts

Posted - 2007-04-18 : 01:14:08
Hi
I want to write all my select ststements using locks
how i should write a select ststement using locks
i searched for examples but iam not getting the syntax
Plz give me one example for select statements using locks

thanks in advance

Malathi Rao

nr
SQLTeam MVY

12543 Posts

Posted - 2007-04-18 : 01:47:52
You mean you want to hold the locks?
It needs to be inside a transaction and coded with holdlock. The locks will be held until the transaction is terminated.

begin tran
select * from tbl with (tablockx, holdlock)
commit tran

Why would you want to do this though? Maybe a bit of thought would give you a better way of doing whatever it is.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jogin malathi
Posting Yak Master

117 Posts

Posted - 2007-04-18 : 02:30:02
Select Source_Id,Sname,Type,Parent_Company,Nature_of_Work,Status from Source with(tablock)
where Status=@stat

What is the meaning of above query

quote:
Originally posted by nr

You mean you want to hold the locks?
It needs to be inside a transaction and coded with holdlock. The locks will be held until the transaction is terminated.

begin tran
select * from tbl with (tablockx, holdlock)
commit tran

Why would you want to do this though? Maybe a bit of thought would give you a better way of doing whatever it is.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.



Malathi Rao
Go to Top of Page

jogin malathi
Posting Yak Master

117 Posts

Posted - 2007-04-18 : 02:32:33
Select Source_Id,Sname,Type,Parent_Company,Nature_of_Work,Status from Source with(tablock)
where Status=@stat

What is the meaning of above query

quote:
Originally posted by nr

You mean you want to hold the locks?
It needs to be inside a transaction and coded with holdlock. The locks will be held until the transaction is terminated.

begin tran
select * from tbl with (tablockx, holdlock)
commit tran

Why would you want to do this though? Maybe a bit of thought would give you a better way of doing whatever it is.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.



Malathi Rao
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2007-04-18 : 02:48:48
It'll take a shared lock on the table for the select then release it - doubt if it does what the coder expects.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -