sql - TSQL- Run stored procedure if backup data is less than specified -


some back-story. have test environment houses test data. every night, job copies on database backups production , restores them test. not often, we'll have database backup on few days old having application open.

i'm looking run stored procedure inserts data table, when last database backup time less few days old , if isn't, send out email user. i'd want used scheduled job in sql server. here's query i've found web , have edited our liking:

with recentbackup (   select sdb.name databasename, bus.backup_finish_date lastbackuptime, row_number() on (partition sdb.name order bus.backup_finish_date desc)     1rownum sys.sysdatabases sdb left outer join msdb.dbo.backupset bus on bus.database_name = sdb.name group sdb.name, bus.backup_finish_date )  select databasename, lastbackuptime       ,case        when cast(lastbackuptime date) >= getdate() - 3 1 else 0       end goodbackup   recentbackup   rownum = 1 

if of rows show goodbackup = 1, want stored procedure run, if goodbackup = 0 on of rows, want job fail.

here's example data:

databasename    lastbackuptime              goodbackup database1       2014-08-16 22:00:45.000     1 database2       2014-08-14 22:30:20.000     0 database3       2014-08-16 21:15:07.000     1 database4       2014-08-16 21:25:03.000     1 database5       2014-08-16 21:30:54.000     1 database6       2014-08-16 21:00:03.000     1 

i may on simplifying similar work?

if exists (select goodbackup                 recentbackup                 goodbackup = 1             )  begin exec usp_success_proc end else begin exec usp_issue_email end 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -