2

In C# we can make a set of operations atomic using locks:

lock (syncLock) { // Do something 1 // Do something 2 // ... } 

Now in T-SQL I want the same with a row. I should do some calculations and I want no change to the row until my calculations are finished. I know that the lock should be minimized and I did so.

Also the whole operation is read (no write) so there's no need for a transaction.

LOCK SELECT statement on a single row // Some calculations here... IF ... RETURN End of LOCK 

I know about ROWLOCK and I can use it with my select statement. But I need the lock for calculation lines also. Is there something like C# brackets in T-SQL or BEGIN LOCK, END LOCK just like Monitor enter/exit ?

2 Answers 2

5

BEGIN TRANSACTION TranName;

..your stuff

COMMIT TRANSACTION TranName;

Sign up to request clarification or add additional context in comments.

1 Comment

You say TRANSACTION here works as a lock (the row would not be affected outside while the code is running) although the whole operation is just reading ?!
2

Have a look at sp_getapplock:

http://web.archive.org/web/20140103030643/http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=GetAppLock&referringTitle=Home

http://clay.lenharts.net/blog/2008/01/28/the-sp_getapplock-secret/

2 Comments

Afraid the first link is broken
@SteveC - updated, I recommend the web.archive.org for this sort of problems.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.