Determine LSN of database in NORECOVERY mode

I'm restoring transaction logs to a database, but need a way to know which transaction log I should start with. Typically I'll see something like this:

The log in this backup set terminates at LSN 103000001633300001, which is too early to apply to the database. A more recent log backup that includes LSN 103000002524300001 can be restored.

So how do I get the LSN 103000002524300001 programattically? The database is in NORECOVERY mode so DBCC LOG doesn't work. I've got records of the transaction logs themselves so I know what their first and last LSN's are. I just don't know where the database itself currently sits.


Some lsn columns are in sys.master_files which at the server level

The column "redo_start_lsn" may be just the ticket.

I haven't tried this BTW...


You can get this information with a query such as:

SELECT TOP 1 b.type, b.first_lsn, b.last_lsn, b.checkpoint_lsn, b.database_backup_lsn
FROM msdb..restorehistory a
INNER JOIN msdb..backupset b ON a.backup_set_id = b.backup_set_id
WHERE a.destination_database_name = 'AV_PROD'
ORDER BY restore_date DESC