Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my table has integer column for id

I am using Isnull() function for select maximum id like
SQL
select max(isnull(id,0))+1 from table

But it is not working. When the table is null it displays null null value.
I didn't know Why?

Please help me.
Posted
Updated 15-Aug-12 20:35pm
v2

SQL
select isnull(max(id),0)+1 from table
 
Share this answer
 
Comments
Mehdi Gholam 16-Aug-12 2:35am    
5'ed
StianSandberg 16-Aug-12 2:39am    
perfect :) 5'ed
In your code, you select max(no records), which is (correctly) null.

Use isnull(max(id),0)+1

If the table is empty, max(id) will return null, and isnull will replace it by zero.

Hope this helps,

Pablo.
 
Share this answer
 
If the table contans no records, your query will not return any result rows hence you're not getting any macimum value.

One way to do this is to use a scalar. So for example something like this:
SQL
select coalesce( (select max(id) from YourTable), 0)
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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