Posts

Showing posts from January, 2010

Coding MSSQL Start Job and Wait for it

I've been kicking off SQL Agent Jobs for a long time now, but I've never needed to wait for it to end before.  Until today, I needed to pull data from a Ultra Secure SQL Server database and didn't want my web servers to have access to the ultra secure sql server.  So I decided to create command line SQL Scripts that connected to the secure database and pull data into a "staging" table.  And use a Sql Agent job to run that script.  But using the following SQL kicked off the job, but didn't wait until it was finished. msdb..sp_start_job @job_name = '<job name>' Therefore, I needed a Query that let me know if the job was still running.  The following query checks the activity table to see if one is running and hasn't finished yet, returning job name if it is still running.  select j.[name] from msdb..sysjobs j inner join msdb..sysjobactivity a on a.job_id = j.job_id where j.name = '<job name>' AND a.st...