Retrieve the last record added

Posts   
 
    
JayBee
User
Posts: 282
Joined: 28-Dec-2006
# Posted on: 28-Mar-2009 15:36:11   

Hi,

I have to create some periodic reports that require a lot of record processing. To lighten this process I have decided to run a service on a daily basis in which I totalize various fields that I need in the periodic reports. Instead of building the reports from the basic data, I create the reports using the day-totals.

To determine the day(s) the service has to accumulate the data for, I want to retrieve the last record added to the daily record set and obain the date it concerns (is one of the fields in the record). Is there an easy way to retrieve the the last records from the file. I.e. without retrieving a collection of 1 that is sorted backwards based on the date?

Or should I maintain the last day in some kind of parameter database?

Best regards,

Jan

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Mar-2009 19:09:07   

What you have to think is what "last record" means. If that means that is the record with the newest "some date field" then the only way I can think is order descending and limit to 1 result.

You could also use your second option. store the parameter somewhere. Be aware of the concurrency aspects of this.

David Elizondo | LLBLGen Support Team
JayBee
User
Posts: 282
Joined: 28-Dec-2006
# Posted on: 29-Mar-2009 22:12:35   

I already have implemented the descending order and limit the result to 1. What I have not mentioned is that the field I'm interested in, is an Identifier field that is automatically incremented bij SQL Server each time a record is added. I assume that the latest record added is maintained somewhere in the master-database. I thought perhaps LLBLGen is capable of supplying it as a class (static) method.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Mar-2009 10:42:23   

I don't understand your requirement.

Maybe you need to sort on the Id Descendingly and limit to 1. This would bring the last record added if the Id is an Identity field.

JayBee
User
Posts: 282
Joined: 28-Dec-2006
# Posted on: 30-Mar-2009 20:07:36   

I'll try to clarify this.

In SQLServer when defining a table, you can set a field to be a Primary Key. When this field is an integer, you can also set the Identity Specification to Yes. You can also specify a start value and an increment value.

Every time a record is added to this table, SQLServer determines the new key value by incrementing the identity field of the last record with the increment value specified.

SQLServer holds the last issued key value for this table somewhere in the master database. Therefor it should be possible to retrieve this value without accessing the table. I need the last record added, and therefor wanted to access it directly on its PK.

The question is:

Can LLBLGen provide the last key stored in case the table has Identity Specification set to Yes?

DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 30-Mar-2009 21:28:03   

Nope, that's impossible to question the Master database about this fact.....but you can of course easily solve this by performing a MAX aggregate on this specific table to retrieve the highest (and latest) added record. After you insert a new record (entity), you can also directly retrieve the assigned PK from the entity.

grtz, Danny