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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Lock and insert

Author  Topic 

dnagahawatte
Starting Member

24 Posts

Posted - 2010-04-03 : 08:17:33
Hi
I have a calls table where i have to insert records to it from a webservice. Call_ref is the primary key. To find the next Call_ref. i do Select max(Call_Ref)+1 from calls.
Now i need to lock/reserve this Call_ref untill i insert date. This is a bulk insert as well.
Otherwise another user might insert a call with that number directly using the system.
Can you please HELP?
Thanks you

Kristen
Test

22859 Posts

Posted - 2010-04-03 : 08:36:56
I think your best bet would be to create a table to store the "Next available number", and then update that when you want a new number.

Any other user / process can then also get a "next number" and will not block other users / processes getting numbers.

The only downside is that if you do no use your number (application crashes / user changes their mind) then there will be a gap in the numbers. However, this is "normal" in systems that use IDENTITY columns too, so best to just accommodate it if there is any issue with gaps in your organisation.

The other benefit is that instead of getting the "next available number" (i.e. just add one to the current value) you can also get a range of next-numbers (add N to the current value), and then you can use that range of numbers for a batch-insert.
Go to Top of Page

dnagahawatte
Starting Member

24 Posts

Posted - 2010-04-03 : 10:37:08
What if i get the next available number with in the same stored procedure that i use to insert data.?
Can another process triger (take that number) while my stored procedure running? Can insert data to a table happen concurrently??
Please advice..

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-03 : 12:26:34
I don't understand why an IDENTITY field isn't being used instead. It doesn't suffer from this issue of having to lock the value.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-04-03 : 13:06:34
I agree Tara, but if I understood the O/P correctly he wants to get the number "up front" of the insert (I expect that could be worked around, but sometimes its nice to give the number on a data entry screen for the operator to use on paper-based system, or where importing a batch of records (harder to deduce the IDENTITY's that have been allocated to a whole batch)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-03 : 13:16:07
If that's the case, then you can do a dummy insert using IDENTITY, grab the value, and then update that record with the correct data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dnagahawatte
Starting Member

24 Posts

Posted - 2010-04-03 : 13:42:03
Cant answer that question, cos ,System is already in place.(developped ages ago) Im trying to get calls from another database using webservices and insert in to this database.
Will my Stored procedure option work??
Go to Top of Page

dnagahawatte
Starting Member

24 Posts

Posted - 2010-04-03 : 15:40:32
HOW abt TABLOCKX ??
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-03 : 18:49:40
If you want to put an exclusive lock on the table, then yes that'll work. But no queries on that table will work until the lock is released.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-04-04 : 05:19:05
I wouldn't use a database lock that was depending on some client-side application, or worse - a "person"!! - making a decision / further processing.
Go to Top of Page
   

- Advertisement -