select maximum in a table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalini166
    New Member
    • Apr 2008
    • 70

    select maximum in a table

    Hi

    i have table empid,empname.. .........etc.In my webform i have empname etc.but not empid.

    i empid is inserted based on the maximum of the empid in the table.At first my table was empty.then how should i find maximum of table.


    pls help me.
  • antonopn
    New Member
    • Mar 2008
    • 42

    #2
    Try this
    Code:
    select top 1 from mytable order by empid desc
    This will give you the bigest empid number!

    Comment

    • deric
      New Member
      • Dec 2007
      • 92

      #3
      You can also make use of the MAX function...

      Comment

      • Delerna
        Recognized Expert Top Contributor
        • Jan 2008
        • 1134

        #4
        Originally posted by deric
        You can also make use of the MAX function...
        and a benefit for that is you can set a starting value for when the table is empty.
        Suppose that a number sequence should start at 1000 an the table is empty then
        [code=sql]
        SELECT isnull(max(id), 1000) as id
        FROM YourTable
        [/code]

        Comment

        Working...