SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ROWLOCK


ROWLOCK

Author
Message
ESAT ERKEC
ESAT ERKEC
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 939
CREATE TABLE [dbo].[TESTROWLOCK](
[ID] [int] NULL,
[NAME] [varchar](100) NULL,
[SURNAME] [varchar](200) NULL
) ON [PRIMARY]

GO

TAB -1
I m trying
BEGIN TRAN

SELECT ID FROM TESTROWLOCK WITH(ROWLOCK) WHERE ID=2


TAB-2


SELECT ID FROM TESTROWLOCK WHERE ID=2


I want to ask that tab-2 select is give result but tab-1 is not commited why rowlock hint does not lock that rows
Orlando Colamatteo
Orlando Colamatteo
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14795 Visits: 14396
ESAT ERKEC (3/19/2013)
CREATE TABLE [dbo].[TESTROWLOCK](
[ID] [int] NULL,
[NAME] [varchar](100) NULL,
[SURNAME] [varchar](200) NULL
) ON [PRIMARY]

GO

TAB -1
I m trying
BEGIN TRAN

SELECT ID FROM TESTROWLOCK WITH(ROWLOCK) WHERE ID=2


TAB-2


SELECT ID FROM TESTROWLOCK WHERE ID=2


I want to ask that tab-2 select is give result but tab-1 is not commited why rowlock hint does not lock that rows

ROWLOCK just opens a shared lock which is compatible with other shared locks.

For locking a row based on a SELECT you would need to ask for WITH (UPDLOCK, TABLOCK) which takes an exclusive table lock, or you could use the SERIALIZABLE isolation level in the window with your transaction. Both options have high safety for your rows, but are very low on concurrency allowances.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
AJ@SQL
AJ@SQL
SSC-Enthusiastic
SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)

Group: General Forum Members
Points: 101 Visits: 309
You may want to read more regarding ROWLOCK.
What I have read is: Row locks are not taken unless ROWLOCK is combined with other table hints that require locks, such as UPDLOCK and HOLDLOCK.
ESAT ERKEC
ESAT ERKEC
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 939
In my application ı set the transaction first

BEGIN TRAN
UPDATE TESTROWLOCK SET ID=ID WHERE ID=2

SELECT ID FROM TESTROWLOCK WHERE ID=2

ı want to change

this

BEGIN TRAN

SELECT ID FROM TESTROWLOCK (hint row base lock ?) WHERE ID=2
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86026 Visits: 45226
AJ@SQL (3/19/2013)
What I have read is: Row locks are not taken unless ROWLOCK is combined with other table hints that require locks, such as UPDLOCK and HOLDLOCK.


That's not true. The Rowlock hint alone means that SQL will take whatever type of locks it needs (shared in this case) at the row level to start, potentially escalating to table if it needs.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


AJ@SQL
AJ@SQL
SSC-Enthusiastic
SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)

Group: General Forum Members
Points: 101 Visits: 309
Thanks! Probably I misunderstood. thanks for clarification.
ESAT ERKEC
ESAT ERKEC
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 939
I think ı can not change this sql script to lock that rows and then do something
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86026 Visits: 45226
Not sure what you're trying to do. A select by default takes shared locks, those locks are released as soon as the row is read (in the default isolation level anyway). Also, shared locks don't block shared locks, so two selects against the same table won't block each other.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


ESAT ERKEC
ESAT ERKEC
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 939
I m trying to do that

I want to be sure

BEGIN TRANSACTION

SELECT * FROM SomeRecords where id=1

............
Some Select and if statment
..........

UPDATE SomeRecords set=somerow where id=1

UPDATE SomeRecords

COMMIT TRAN

I have to sure while the commit transaction no body change my records ı m trying to lock that records
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86026 Visits: 45226
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ before the begin tran.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search