sql server - SQL query to find running queries and jobs -
sql server - SQL query to find running queries and jobs -
i new sql. using sql server 2008.
what wanted have query can set schedule of etl-job forcing wait until server running given job idle in terms of none jobs or queries running.
i have written statement , wanted know if can give me input on validity of query. have tested however, , seems work.
so got stement:
declare @retcode int declare @jobstatus int declare @querystatus int set @jobstatus = -1 set @querystatus = -1 while (@jobstatus != 0 , @querystatus != 1) begin -- count running jobs select @jobstatus = count(*) ( select sj.name ,datediff(second,aj.start_execution_date,getdate()) seconds msdb..sysjobactivity aj bring together msdb..sysjobs sj on sj.job_id = aj.job_id aj.stop_execution_date null -- job hasn't stopped running , aj.start_execution_date not null -- job running --and sj.name = 'jobname' , not exists( -- create sure recent run select 1 msdb..sysjobactivity new new.job_id = aj.job_id , new.start_execution_date > aj.start_execution_date ) ) a; --only if other jobs running if (@jobstatus = 0) begin -- querystatus zähl alle laufenden queries die eigene abfrage wird auch gezählt deswegen oben !=1 select @querystatus = count(*) ( select sqltext.text, req.session_id, req.status, req.command, req.cpu_time, req.total_elapsed_time sys.dm_exec_requests req cross apply sys.dm_exec_sql_text(sql_handle) sqltext req.status not in ('background','sleeping','suspended') ) b; end waitfor delay '00:00:02' end -- output = 1 when there no jobs or queries running return(1)
sql-server etl
Comments
Post a Comment