Is there a way to start a SQL Server agent job if a table is not empty?
Basically, first I want to check a table, and if the table is not empty then start the job. If it is empty, then check every 1 hour to see whether it has the required information.
I want to run this job once a day once I have data in the table.
Best Answer
Not directly, no. What you describe is kind of like expecting your e-mail program to know when to open and display new e-mail, even though the program needs to be open already to check for new mail.
You might be able to do this with a trigger on the table itself - however note that triggers can be disabled, and also that some operations (such as
BULK INSERT
) can intentionally bypass triggers.(This doesn't follow your requirement of checking once an hour, but rather it responds to the first insert. You'll also want to drop the trigger once the job has been enabled, but I don't know that you'll be able to do that from within the trigger.)
You could probably create a different job that wakes up every hour. It would check the data, and if it is time to enable the other job, it would do so (similar to the trigger above) and also disable itself. You could also schedule something similar outside of SQL Server (e.g. using PowerShell, VBScript via Windows Task Scheduler, etc.).
Maybe Event Notifications could assist in a similar way, but I've never used them for this specific purpose.