Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 using max() in insert staement?

Author  Topic 

Xorbit
Starting Member

3 Posts

Posted - 2009-01-04 : 16:12:26
Hi,

I keep track of my own "rank" property to have my lists sorted the right way.
I have a lot of inserts where I want to insert a post with "rank" to be the highest allready in the table +1.
Is there a smart way to do this or do I have to make a select statement first to select out to my ASP what is the max(rank) and then insert it?

I have tried something like
INSERT INTO table (att1, ranken) VALUES ('1', max(ranken)+1);
But that doesn't work out.

Best regards
Klaus

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-04 : 16:53:44
INSERT INTO table (att1, ranken)
SELECT '1', MAX(ranken) + 1
FROM table

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-01-05 : 02:19:36
INSERT INTO table (att1, ranken)
SELECT '1', COALESCE(MAX(ranken),0) + 1
FROM table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -