Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to get Max Count in sql server 2008 using asp .net and atleast Lock it so that at the same time other user cannot get the Max from the same table

What I have tried:

SQL
Select Max(Column) as  test From Table
Posted
Updated 31-Aug-18 1:55am
v5
Comments
Herman<T>.Instance 31-Aug-18 6:47am    
What do you mean by deleting? You are calling a function!


you can find that rowcount value in system tables:
select t.name, p.rows from sys.tables as t
JOIN sys.partitions AS p
ON t.[object_id] = p.[object_id]
WHERE t.name = '<your table name here>'
Herman<T>.Instance 31-Aug-18 7:27am    
Since you changed your question it is now clear what your problem is. You are trying to set a unique ID in a table in the database. Set the ID field als Identity field. Than the DB will pass unique ID's and your ASP.NET does not have to do obscure things.

It sounds like you are planning on using this to generate something like an ID - that's a very bad idea. Never try to "preassign" ID values, that always gives problems in production that are extremely difficult to work out, and even harder to undo once your database has lost integrity as a result.

Use an IDENTITY field if you want almost sequential ID's, or UNIQUE IDENTITY fields if you want to pre-assign them. That way, the system takes care of the "uniqueness" of the values and you don't have to worry.
 
Share this answer
 
It sounds as if you want an exclusive lock on the table while you are getting the max - you can't prevent another user from running the Max function specifically (as far as I know).

To get an exclusive lock use
SQL
SELECT 1 from [Table] WITH (TABLOCKX)
SELECT MAX([Column]) as test from [Table]
But not that other users could still query the table by using WITH (NOLOCK) or SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

[EDIT] - I've just seen the comment from Digimanus. Do not try to use Max to create a unique ID. Use an Identity[^] column or uniqueidentifier[^]
 
Share this answer
 
v2
Quote:
How to get Max Count in sql server 2008 using asp .net and atleast Lock it so that at the same time other user cannot get the Max from the same table

You are on wrong track.
SQL server is build with multi-user in mind, so it is provided with tools to give unique id to records.
Just declare id as autoincrement:
SQL AUTO INCREMENT a Field[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900