Results 1 to 2 of 2

Thread: Find out maximum sequence number?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Find out maximum sequence number?

    In SQL server, the following code is works to find out maximum number in SEQUENCE column.

    SEQUENCE column is int type increasing by 1

    select max(SEQUENCE) as MYSEQ from Order where ordernumber ='20140710'

    How to run this code in vb.net to get maximum SEQUENCE number?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Find out maximum sequence number?

    Something like this should work:

    Code:
    Dim yourConnectionString As String = <whatever your connection string is>
    Dim retVal As Integer
    
    Using cn As New SqlClient.SqlConnection(yourConnectionString)
     Using cmd As SqlClient.SqlCommand = cn.CreateCommand
      Try
        cn.Open
        cmd.CommandText = "select max(SEQUENCE) as MYSEQ from Order where ordernumber ='20140710'"
        Dim obj As Object = cmd.ExecuteScalar
        If obj Isnot Nothing then
         retVal = CInt(obj)
        Else
         'There were no records, so what do you want to do?
        End If
      Catch ex As Exception
       'Do what you want. The most likely exception is that the DB is unavailable.
      End Try
     End Using
    End Using
    
    Return retVal
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width