Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

please How to do:

Select (MAX(ID)+1) from table;


thanks in advance.
Posted

If you have multiple client access at the same time you will have race conditions and you will produce duplicate next IDs in that case.

Start a serializable transaction and then compute and use the largest number to avoid duplication.

Read the answer on this page to get the idea :
How to autoincrement in SQL[^]


For more information about transactions :
http://msdn.microsoft.com/en-us/library/ms188929.aspx[^]

http://msdn.microsoft.com/en-us/library/ms173763.aspx[^]

http://msdn.microsoft.com/en-us/library/2k2hy99x%28v=vs.71%29.aspx[^]

Hope it helps.
 
Share this answer
 
SQL
SELECT MAX(COLUMNNAME) + 1 AS Result FROM TableName

The datatype of column should be numeric/int to product exact result
 
Share this answer
 
Comments
Member 12968503 31-Mar-17 5:13am    
what if its NULL
koolprasad2003 3-Apr-17 0:49am    
As I said, 'The datatype of column should be numeric/int to product exact result' if it has NULL value, it return MAX as 0 and add 1 in it.
So there is no issue if it has NULL value
hi,

try to check following link:
http://msdn.microsoft.com/en-us/library/ms187751.aspx[^]

here you can find explanation of Max TSQL function. But can you tell me what do you want to do exactly?

Regards
Robert
 
Share this answer
 
Comments
m_safaaaa 22-Jan-12 23:45pm    
hi, thank you for your answer. what i want to do is:
wrinting one sql query where i select a value incremented by 1 from one table ( in my case the value is equal to MAX(IdProject) ; IdProject is a column in the table PROJECT)

Best Regards

Safa
palraj001 22-Jan-12 23:56pm    
what data type u have used for IdProject?
m_safaaaa 23-Jan-12 0:12am    
int
palraj001 23-Jan-12 0:27am    
then it should increment the value.

check with select Max(IdProject), max(IdProject) + 1 from PROJECT
hi dear friend ,

please try this query

<u>
SELECT MAX(isnull(COLUMNNAME,0)) + 1 AS Result FROM TableName</u>
 
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