I'm working on a job step which needs to reference the Login Name value for the user who started the job. It'll be passed as a value into a table field, which is then referenced by other processes.
I haven't been able to find if or where the starting username is stored in system tables. It must be stored somewhere as the (job outcome) step in msdb.dbo.sysjobhistory references is thusly:
The job succeeded. The Job was invoked by User [domain]\[username]. The last step to run was...
I'd like to capture that [domain]\[username] value for my job step. I know it can be done as a subsequent job, referencing the original and doing some string manipulation, but I'd prefer to do it in the same job, if possible.
Thanks in advance!
That is logged in the message column of the msdb.dbo.sysjobhistory table.
You may want to do some joins to the sysjobs table as well. I.E.
select
j.name as 'JobName',
run_date,
run_time,
msdb.dbo.agent_datetime(run_date, run_time) as 'RunDateTime',
case
when left(h.message,16) = 'Executed as user'
then substring(h.message,19,charindex('.',h.message,1) - 18)
when h.message like '%invoked by%'
then substring(h.message,charindex('invoked by ',h.message) + 11,charindex('.',substring(h.message,charindex('invoked by ',h.message) + 11,99)) - 1)
else h.message
end
From
msdb.dbo.sysjobs j
INNER JOIN
msdb.dbo.sysjobhistory h
ON j.job_id = h.job_id
where
j.enabled = 1 --Only Enabled Jobs
order by
JobName,
RunDateTime desc
REF
we have a sql server agent job which is running two sql servant jobs.The job is designed in the way that the second job will wait till the first job completed. But sometimes the wait is not working and it causes both jobs ends up running at same time. This causes failure of both jobs. We uses below code for waiting mechanism.
WHILE
(
SELECT COUNT(*) AS cnt
FROM msdb.dbo.sysjobs J
INNER JOIN msdb.dbo.sysjobactivity JA
ON J.job_id = JA.job_id
WHERE start_execution_date IS NOT NULL
AND stop_execution_date IS NULL
AND J.name = "first job name"
AND last_executed_step_date > DATEADD(DD,-1,GETDATE())--//Added to bypass historical corrupt data
) > 0
BEGIN
--WAIT
WAITFOR DELAY '00:01';
END
any suggestion why this is not working sometimes?
I have a situation where I have 1 package (Package A) that take about 3 min to complete and another package (Package B) that takes about 4 min to complete. I need package A to run every 5 min via a job and package B to run every 15 min. However package B can only run after package A has been completed.
Example
5- Package A runs
10- Package A runs
15- Package A runs and then Package B
20- Package A runs
25- Package A runs
30- Package A runs and then Package B
Is there any way to schedule jobs to meet the requirements above?
Really appreciate your help. Thanks in advance!
for package B i would just put in logic to check if package A has stopped running
the wait script would be
WHILE EXISTS (
SELECT
*
FROM
msdb.dbo.sysjobs_view job
JOIN
msdb.dbo.sysjobactivity activity
ON
job.job_id = activity.job_id
JOIN
msdb.dbo.syssessions sess
ON
sess.session_id = activity.session_id
JOIN
(
SELECT
MAX( agent_start_date ) AS max_agent_start_date
FROM
msdb.dbo.syssessions
) sess_max
ON
sess.agent_start_date = sess_max.max_agent_start_date
WHERE
run_requested_date IS NOT NULL AND stop_execution_date IS NULL
AND job.name = 'package A')
BEGIN
WAITFOR DELAY '00:00:30';
END
This would check every 30 seconds if package A has stopped running.
Alternatively, you create a table that would keep track of job status and have A write to it to keep track of the status.
I am not sure what is going on? I still get an email even though the job stopped running at 11:50 pm which clearly is less than thean 7 hours being that it started at 7 pm last night..It is suppose to email me if the job runs longer than 7 hours and for some reason yesterday it worked in test but today its not..Any ideas???
SELECT *
FROM msdb..sysjobactivity aj
JOIN msdb..sysjobs sj on sj.job_id = aj.job_id
WHERE DATEDIFF(HOUR,aj.start_execution_date,GetDate())> 7
AND aj.start_execution_date IS NOT NULL
AND sj.name = 'Nightly_Job'
and not exists ( -- make sure this is the most recent run
select 1
from msdb..sysjobactivity new
where new.job_id = aj.job_id
and new.start_execution_date > aj.start_execution_date)
if ##ROWCOUNT > 0
BEGIN
USE msdb
EXEC sp_send_dbmail
#profile_name = 'DB_Mail',
#recipients = 'xxx#yyy.com',
#subject = 'T-SQL Query Result',
#body = 'The Nightly_Job has been running 7
The problem with your query is that it only checks how long has passed since the job was started... it doesn't take into account when the job completed.
If there are no other executions of the job within 7 hours, then your check that limits the query to the most recent run doesn't exclude the previously completed job... your query then returns the job that was started over 7 hours ago, but in this case completed within 4 hours.
To fix your query to exclude completed jobs, modify your query as follows:
SELECT *
FROM
msdb..sysjobactivity aj
JOIN msdb..sysjobs sj on sj.job_id = aj.job_id
WHERE
DATEDIFF(HOUR, aj.start_execution_date,
ISNULL(aj.stop_execution_date, GetDate()) )> 7
AND sj.name = 'Nightly_Job'
AND NOT EXISTS ( -- make sure this is the most recent run
SELECT 1
FROM msdb..sysjobactivity new
WHERE new.job_id = aj.job_id
AND new.start_execution_date > aj.start_execution_date)
This question already has answers here:
List the queries running on SQL Server
(17 answers)
Closed 7 years ago.
Is there a program or a sql query that I can find which SQL queries are being run on an SQL Server 2012? I think there was a tool in earlier version of SQL Server where the actual query content gets displayed or the stored procedure name?
I use the below query
SELECT SPID = er.session_id
,STATUS = ses.STATUS
,[Login] = ses.login_name
,Host = ses.host_name
,BlkBy = er.blocking_session_id
,DBName = DB_Name(er.database_id)
,CommandType = er.command
,ObjectName = OBJECT_NAME(st.objectid)
,CPUTime = er.cpu_time
,StartTime = er.start_time
,TimeElapsed = CAST(GETDATE() - er.start_time AS TIME)
,SQLStatement = st.text
FROM sys.dm_exec_requests er
OUTER APPLY sys.dm_exec_sql_text(er.sql_handle) st
LEFT JOIN sys.dm_exec_sessions ses
ON ses.session_id = er.session_id
LEFT JOIN sys.dm_exec_connections con
ON con.session_id = ses.session_id
WHERE st.text IS NOT NULL
Depending on your privileges, this query might work:
SELECT sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
Ref: http://blog.sqlauthority.com/2009/01/07/sql-server-find-currently-running-query-t-sql
There's this, from SQL Server DMV's In Action book:
The output shows the spid (process identifier), the ecid (this is similar to a thread within the same spid and is useful for identifying queries running in parallel), the user running the SQL, the status (whether the SQL is running or waiting), the wait status (why it’s waiting), the hostname, the domain name, and the start time (useful for determining how long the batch has been running).
The nice part is the query and parent query. That shows, for example, a stored proc as the parent and the query within the stored proc that is running. It has been very handy for me. I hope this helps someone else.
USE master
GO
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT
er.session_Id AS [Spid]
, sp.ecid
, er.start_time
, DATEDIFF(SS,er.start_time,GETDATE()) as [Age Seconds]
, sp.nt_username
, er.status
, er.wait_type
, SUBSTRING (qt.text, (er.statement_start_offset/2) + 1,
((CASE WHEN er.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
ELSE er.statement_end_offset
END - er.statement_start_offset)/2) + 1) AS [Individual Query]
, qt.text AS [Parent Query]
, sp.program_name
, sp.Hostname
, sp.nt_domain
FROM sys.dm_exec_requests er
INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle)as qt
WHERE session_Id > 50
AND session_Id NOT IN (##SPID)
ORDER BY session_Id, ecid
here is what you need to install the SQL profiler http://msdn.microsoft.com/en-us/library/bb500441.aspx. However, i would suggest you to read through this one http://blog.sqlauthority.com/2009/08/03/sql-server-introduction-to-sql-server-2008-profiler-2/ if you are looking to do it on your Production Environment.
There is another better way to look at the queries watch this one and see if it helps
http://www.youtube.com/watch?v=vvziPI5OQyE
The tool is called the SQL Server Profiler, it's still part of the standard toolset.
You are talking about SQL Profiler.