Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL MAX() function question

Status
Not open for further replies.

dwfresh

Programmer
Oct 10, 2002
10
US
I know this is a pretty simple question, but I'm a rookie at SQL.
I have a column named 'rowOrder' in my table. When a user enters a new item/row into the table, I want SQL to get the highest number in 'rowOrder'column, and add 1, and set that new 'rowOrder' field to that number.
Can someone show me the SQL language that will accomplish this ?

I tried something similar to
Code:
INSERT INTO mytable(rowOrder) VALUES MAX(rowOrder) + 1
but no luck.
Am i on the right track ?

Thanks very much !!
 
Ok, MAX is used in to retrieve the Highest Non-null value from a specific column.

Now I would recommend this, you must obtain the MAX value inorder to add 1 to it and then insert it. Right? So to do this I would use a statement like this;

INSERT INTO mytable(rowOrder)
SELECT MAX(rowOrder) + 1 FROM mytable

And if you want a really cheap and good book to learn some SQL from I recomment SAMS Teach yourself SQL in 10 minutes. But since you didn't ask that you can ignore that. :)

Yeah, yeah I know folks, don't flame me, but once in awhile there comes along a book in a condensed format that really is good. This being on of them. :)

Hope this helps!

_________________
Paladine

"Doing what others find difficult is talent.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top