Obtain most recent entry from table with no date field

תשובה Obtain most recent entry from table with no date field

  • Friday, December 28, 2012 2:21 PM
     
     

    I have a table which has guid as the primary id and no date field to indicate when the record was inserted/modified.

    Is there a way to obtain the most recent record inserted? as sorting GUIDs does not seem to guarantee that the entries are in modified order.

    Sample table structure 

    ID(uniqueidentifier), lastname(varchar),firstname(varchar),birthdate(datetime).

    Thanks.

    • Edited by zoollu Friday, December 28, 2012 2:48 PM Correcting mistake in the birthdate field type as pointed out by scott
    •  

All Replies

  • Friday, December 28, 2012 2:35 PM
     
     

    Hi zoollu,

    Try this

    DECLARE @firstname AS VARCHAR (50);
    DECLARE @lastname AS VARCHAR (50);
    DECLARE @BirthDate AS VARCHAR (50);

    SELECT @firstname = firstname,
                @lastname = lastname,
                @BirthDate=birthdate
    FROM   MyTable;     --Add your table name here

    SELECT @firstname,
           @lastname,
           @BirthDate;

  • Friday, December 28, 2012 2:40 PM
     
     Proposed
    No - there is no guaranteed way.  I'll also point out that storing birthdate as varchar is a foolish decision.
  • Friday, December 28, 2012 2:42 PM
     
     

    Sorry I have not tried this but I want to select the most recent record that was inserted or modified. This query seems to select records but not in the order they were last touched or inserted.

    Thanks.

  • Friday, December 28, 2012 2:47 PM
     
     
    Sorry birthdate in datetime field. It was my copy/paste mistake.
  • Friday, December 28, 2012 2:53 PM
     
     Answered

    I don't think there is way unless you don't have a a identity or date column

    Regards
    Satheesh

    • Marked As Answer by zoollu Friday, December 28, 2012 4:40 PM
    •  
  • Friday, December 28, 2012 2:56 PM
     
     
    birthdate is a date,  not a datetime (unless you are using this value in a particular manner - which is unlikely).
  • Friday, December 28, 2012 3:15 PM
     
     
    birthdate is a date,  not a datetime (unless you are using this value in a particular manner - which is unlikely).
    It is SQL server 2005 so there is no date field. This is not something I created though. 
  • Friday, December 28, 2012 3:22 PM
     
     Answered

    Having said, no identity and createddate feild present, there is no way to get the latest record.

    You may create trigger(not a good idea) or progrmatically log the record for the same.


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked As Answer by zoollu Friday, December 28, 2012 4:40 PM
    •  
  • Friday, December 28, 2012 3:37 PM
    Moderator
     
     
    Unless you add a LastModifiedDate field, there is no way to determine the last row.
  • Friday, December 28, 2012 4:32 PM
     
     

    Is there any prob. to add " date field to indicate when the record was inserted/modified" ?


    Ahsan Kabir Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread. http://www.aktechforum.blogspot.com/

  • Friday, December 28, 2012 4:40 PM
     
     

    Is there any prob. to add " date field to indicate when the record was inserted/modified" ?


    Ahsan Kabir Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread. http://www.aktechforum.blogspot.com/

    Yes this is a legacy application which is already in production for long time. I will need to create a tool which needs to obtain the most recent entry to satisfy a requirement. I had my doubts that this is not possible which is now confirmed from the help I got here.

    Thanks all for the answers.

  • Saturday, December 29, 2012 6:10 AM
     
     

    Hi zoollu,

    I thought u just want the last inserted record.

    You can not get last modified record with this existing structure.

    Either you have to create another transaction table which can keep track of modified data of your existing column with guid fk in it .

    or you have to add modified date column that you are saying not an option .....