Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005
In my table has integer column for id
 
I am using Isnull() function for select maximum id like
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 15-Aug-12 20:26pm
devausha1.4K
Edited 15-Aug-12 20:35pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

select isnull(max(id),0)+1 from table
  Permalink  
Comments
Mehdi Gholam at 16-Aug-12 2:35am
   
5'ed
AlluvialDeposit at 16-Aug-12 2:39am
   
perfect :) 5'ed
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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:
select coalesce( (select max(id) from YourTable), 0)
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 878
1 OriginalGriff 410
2 CPallini 275
3 George Jonsson 226
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,482
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 16 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100