Get last record in a SQL database

Don't know if this is the right SQL section, but... using VBE, how do you SQL for the last record in the database

Thanks.


Answer this question

Get last record in a SQL database

  • Vishal Verma

    I'd recommend using the IDENT_CURRENT function. However it is limited to one table. So if you're looking for the last entered id across all tables...well it's a bit more complex. Unless you use some kind of scheme that identifies what table an ID belongs too. I've seen this used at companies where they have a single generator for all IDs. But I digress.

    Select * from table_name where id=IDENT_CURRENT('table_name')

    Should do what you want.



  • David NL

    > the LAST record entered in the database.... if for example if you
    > entered 10 records in a new database, how do you fetch #10 - or the LAST
    > recorded
    > entered (without indicating 'get the 10th record') 
    
    Again, SQL Server doesn't keep track of which row is the newest. This is 
    not Access, and you are not stuffing a line of data at the end of a text 
    file.
    
    What does your table structure look like  Do you have a naturally 
    increasing column (such as IDENTITY, or DATETIME reflecting when the row was 
    created) 
    
    A 
    
    
    


  • T. J. Bussler

    What does "last" mean A table is an unordered set of rows, so you need to indicate to SQL Server what you mean by "last"... wrote in message news:c6e41dc5-463e-4bfd-b88c-99ebbc38e746@discussions.microsoft.com... > Don't know if this is the right SQL section, but... using VBE, how do > you SQL for the last record in the database > > Thanks. >
  • Ghalib

    > the LAST record entered in the database.... if for example if you
    > entered 10 records in a new database, how do you fetch #10 - or the LAST
    > recorded
    > entered (without indicating 'get the 10th record') 
    
    Again, SQL Server doesn't keep track of which row is the newest. This is 
    not Access, and you are not stuffing a line of data at the end of a text 
    file.
    
    What does your table structure look like  Do you have a naturally 
    increasing column (such as IDENTITY, or DATETIME reflecting when the row was 
    created) 
    
    A 
    
    
    


  • rlieving

    That was really helpfull Thanks

  • Rick112

    This is the answer. Thanks to you Edward E. Weller



  • Javier Carvajal

    the LAST record entered in the database.... if for example if you entered 10 records in a new database, how do you fetch #10 - or the LAST recorded
    entered (without indicating 'get the 10th record')  

  • Andrey Egorov

    I have a ID that numbers each record (key).... I was thinking along the line of
    SELECT MAX id but wasn't sure if there was a better way of doing so.

  • Jamie Cool

    This is the answer... Thanks to you Edward E. Weller


  • CraigSBoyd

    If you want to select the last ROW inserted in a TABLE in a DATABASE that has an IDENTITY column named ID, you could use the following:

    SELECT *
    FROM    TABLE
    WHERE  ID = IDENT_CURRENT('TABLE')

    or

    SELECT *
    FROM    TABLE
    WHERE   ID = (SELECT MAX(ID)  FROM TABLE)

    I hope this helps.

    Sincerely,
    Edward E. Weller

  • Jeff Micro

    select top 1 * from TABLE_NAME  order by ID desc

    isn't is what you're asking for

  • Sebastien

    athanks to all fo royu help I got it



  • bmkiss67

    Dear Valeriy,

    Your reply has helped me a lot !!!! I have been searching for this answer since a long time and didn't get any answer from any where !!!!I am extremely thankful to you.

    May God Bless you always

    Thanks

    Maleeha


  • JeffWoodard

    Thanks.  I haven't yet been able to try all the great ideas I got... still wasn't
    able to open the database... so I'm reading a book.

  • Get last record in a SQL database