retrieve a last value from sql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ahmedhussain
    New Member
    • Dec 2008
    • 79

    retrieve a last value from sql

    Hi,
    Can any one tell me .. How to retrieve a last value from the database?
    I tried the last function, but I just got the error.

    Thanks in advance

    Regards,
    Syed Ahmed Hussain
  • ziycon
    Contributor
    • Sep 2008
    • 384

    #2
    Could try the below, not sure if it works in SQL though.
    Code:
    SELECT MAX(column) FROM tablename

    Comment

    • Jerry Winston
      Recognized Expert New Member
      • Jun 2008
      • 145

      #3
      Correct me if i'm wrong, but do you mean last row entered into a table?
      Code:
      SELECT MAX(MySequentialIndexColumn) from [MyTable]
      works based on the premise that MySequentialInd exColumn is an IDENTITY column continuously incremented with each addition to the table. Therefore, the highest value of the column should be the latest entry.

      Here's my advice:

      If it is important to know when a row was added to a table the time/date of insertion should be listed as a field. With the addition of a time/date field, you will be able to query your table based on the exact time rows were entered.

      Comment

      • Ahmedhussain
        New Member
        • Dec 2008
        • 79

        #4
        Both of you are right....

        Thank you guys.. :)

        Regards,

        Syed Ahmed Hussain

        Comment

        • OuTCasT
          Contributor
          • Jan 2008
          • 374

          #5
          If you have an identity column then u could use

          Code:
          select @@Identity from myTable

          Comment

          • Ahmedhussain
            New Member
            • Dec 2008
            • 79

            #6
            So what will this query do?
            select @@Identity from myTable

            Comment

            • OuTCasT
              Contributor
              • Jan 2008
              • 374

              #7
              if u have and identity column which set ur rows in order 1 2 3 4 5 etc
              then it will pull the last value inserted in the database.

              i add a index column like so

              Code:
              alter table MYTable
              Add TempID int IDENTITY(1,1)

              Comment

              Working...