LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2007, 11:01 AM   #1
dugas
Member
 
Registered: Jul 2004
Location: louisiana
Distribution: fedora 4/kubuntu
Posts: 116

Rep: Reputation: 15
Locking in Stored Procedure


Hello All,
Basically I am trying to implement assigning of unique consecutive integers from a particular row in a table in MSSQL 2000.

Example pseudocode:

<code>
SET ISOLATION LEVEL REPEATABLE READ

SELECT @VAR1 = NextAvailableNumberColumn FROM TheTable WHERE pk=@pk

UPDATE TheTable SET NextAvailableNumberColumn = @VAR1 + 1 WHERE pk=@pk

RETURN @VAR1

COMMIT
</code>

Will this ensure that each client will always get a unique number, or is it possible that this could return the same number to more than one client? Also, if this works, could someone please explain why. It seems to me that if the tranaction was ran concurrently with two instances, then the first select would issue a shared lock for both transactions, and when the update was issued there would be deadlock because the row would be share locked by the two transactions. Please advise.
 
Old 03-24-2007, 03:32 PM   #2
Crito
Senior Member
 
Registered: Nov 2003
Location: Knoxville, TN
Distribution: Kubuntu 9.04
Posts: 1,168

Rep: Reputation: 53
The usual way of doing this is by creating a scalar function that returns max(col)+1 and assigning that as the default value. Something like:
Code:
CREATE FUNCTION [dbo].[fn1](
)
RETURNS int
AS
BEGIN
	return ((SELECT Max(NextAvailNum) FROM TheTable) + 1)
END
And the table def would look something like:
Code:
CREATE TABLE [dbo].[TheTable](
	[NextAvailNum] [int] NOT NULL CONSTRAINT [DF_Num] DEFAULT ([dbo].[fn1]()),
	[SomeAttribute] [nchar](10) NULL
) ON [PRIMARY]

Last edited by Crito; 03-24-2007 at 03:36 PM.
 
Old 03-25-2007, 11:40 AM   #3
dugas
Member
 
Registered: Jul 2004
Location: louisiana
Distribution: fedora 4/kubuntu
Posts: 116

Original Poster
Rep: Reputation: 15
Concurrency Issue

Thanks for the reply Crito. If I understood correctly, the method you showed is for generating the next consecutive number on an insert. I am sorry if I did not make myself clear in my post. The WHERE clause in the update statement shows that I must keep an "autonumber" for each row in the table. Basically, each row in the table defines a particular range within which consecutive numbers must be generated in. For example: Row 1 = Company A, PO Number Range 1-1000, NextAvailablePONum = 10, Row 2 = Company B, PO Number Range 1001-2000, NextAvailablePONum = 1001. Then when the client generates a PO number for a particular company, this stored procedure gets called, and gets the next available po number from the row, then increments the column value for that row by one. The reason it was not implemented with identity columns is that there are a couple of hundred companies.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting Procedure praveen_repelle Solaris / OpenSolaris 2 02-20-2007 02:01 AM
live cd procedure sailu_mvn Linux - Software 3 08-17-2005 02:00 AM
MySQL Stored Procedure Question?? sehgals Linux - Software 5 01-31-2005 01:54 PM
stored procedure in mysql suchi_s Programming 2 10-01-2004 06:14 AM
Installation Procedure? usman767 Linux - Software 2 06-14-2003 07:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration