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 consecutive 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.
4 1301
You are trying to reinvent a wheel for ancient databases.
Create an identity column and it will do for you automatically.
To declare identity column you should do following:
Create table table_name(
ID int identity (1,1),
next columns.....
)
Good Luck.
That is not acceptable in this case. The reason being it is a legacy app that generates autonumbers for different ranges for many different entities. Basically, if I were to use an identity column, I would have to have a different table for each entitiy, which would mean about 500 different tables.
In this case you can do following:
[PHP]
DECLARE @VAR1 int, @pk int
BEGIN TRAN
SELECT @VAR1 = NextAvailableNumberColumn FROM TheTable WHERE pk=@pk
UPDATE TheTable SET NextAvailableNumberColumn = @VAR1 + 1 WHERE pk=@pk
RETURN @VAR1
COMMIT TRAN[/PHP]
To test if it works, open 2 windows in query analyzer.
In the first window execute everything but the last line, which is COMMIT TRAN.
Go to the second window and try to execute:
[PHP]SELECT * FROM TheTable[/PHP]
It shouldn't work at this point and query will run without displaying any result. Don't stop your query.
Go to the first window and execute last line.
Result in the second window should return immediately.
It mean while you are in transaction nobody would be able to enter a table until it is finished.
Good Luck.
OOPS
I think return should be a lat line:
[PHP]DECLARE @VAR1 int, @pk int
BEGIN TRAN
SELECT @VAR1 = NextAvailableNumberColumn FROM TheTable WHERE pk=@pk
UPDATE TheTable SET NextAvailableNumberColumn = @VAR1 + 1 WHERE pk=@pk
COMMIT TRAN
RETURN @VAR1[/PHP]
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Billy Jacobs |
last post by:
I have a client using IE with a .net web application who
is getting a Concurrency Violation whenever she tries to
update certain records in her database.
The strange thing is that I can open up...
|
by: Suzanne |
last post by:
Hi All
I'm having problems getting my data adapter to throw a concurrency
exception with an INSERT command. I want to throw a concurrency
exception if an attempt is made to enter a row into...
|
by: Robin Tucker |
last post by:
With respect to my (now not so recent) thread on Concurrency, I would like
to run my idea past you gurus to see if its a runner. First, a brief recap:
I have a single user system (one user, one...
|
by: ardin |
last post by:
anyone please help, i am developing and application that uses the update
method of dataadapter (select, update, delete command was set by issuing
the OleDbCommandBuilder)in updating the database. ...
|
by: Robert Schuldenfrei |
last post by:
Hi NG,
I am looking for an opinion here. I am new to C# and SQL, being an old
COBOL hand. I have started into a conversion of an old COBOL ERP system. I
have a number of functions working now...
|
by: Robert Schuldenfrei |
last post by:
Dear NG,
I was about to "improve" concurrency checking with a Timestamp when I
discovered
that my current code is not working. After about a day of beating my head
against the wall, I am...
|
by: Homa |
last post by:
Hi,
I'm thinking what will happen if two users access a page at the same
time. If there are any local variable in the page, will this cause
concurrency problem?
Simarily, if this page need to...
|
by: Anthony Paul |
last post by:
Hello everyone!
I've been reading a great book on SOA called "Enterprise SOA" and
found that it answered many questions. However, there's one particular
scenario (involving
business-rules)...
|
by: Rotsey |
last post by:
Hi,
I have a Access 2003 talking to SQL 2005 express DB via
linked tables.
I also have .NET forms application that also talks to the
SQL DB.
I want to know how to handle concurrency in the...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |