SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Getting sql server job status into a table


Getting sql server job status into a table

Author
Message
satish._dba
satish._dba
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 168
Hi Guys,

1) I need to insert a single job status after completing every run into a new table

and

2) Total row count of single table into another new table .

Could any one of you give me thoughts on how to do it..

im kind of struct , cant think of how to do it..

thanks a lot for your time
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34079 Visits: 16649
(1) This information is already stored in sysjobhistory in msdb.
(2) Use the COUNT agrregrate function.

If you need more detailed help than that, please specify your exact requirement, including table DDL, sample data and expected results.

John
satish._dba
satish._dba
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 168
Hi John,
Thanks for the reply..

Im not sure on how to use sysjobhistory data..
and

My request is job named "reorganize" which is in scheduled for daily at 8 Pm.
User want to know the wheather job has failed or successfull..

for this, we want to create a new table and insert the status like below

for this expected results should be

select * from jobstatustable
====================
"Job name" "Status"

reorganize Success

and

2nd request is .. Just want to select row count of emp tabel for example ( User should'nt be granted any access to source table , thats why we want to create new table and insert the total rows of source table into destination table)

ex:

select count(*) from emp;
===========

Row count

1056
.

Please let us know if I can give anymore info...

Thanks
Tava
Tava
Right there with Babe
Right there with Babe (749 reputation)Right there with Babe (749 reputation)Right there with Babe (749 reputation)Right there with Babe (749 reputation)Right there with Babe (749 reputation)Right there with Babe (749 reputation)Right there with Babe (749 reputation)Right there with Babe (749 reputation)

Group: General Forum Members
Points: 749 Visits: 806
satishdbaid (11/19/2012)
Hi John,
Thanks for the reply..

Im not sure on how to use sysjobhistory data..
and

My request is job named "reorganize" which is in scheduled for daily at 8 Pm.
User want to know the wheather job has failed or successfull..

for this, we want to create a new table and insert the status like below

for this expected results should be

select * from jobstatustable
====================
"Job name" "Status"

reorganize Success

and

2nd request is .. Just want to select row count of emp tabel for example ( User should'nt be granted any access to source table , thats why we want to create new table and insert the total rows of source table into destination table)

ex:

select count(*) from emp;
===========

Row count

1056
.

Please let us know if I can give anymore info...

Thanks



I know this is not exactly what you are trying to do but there are simpler ways to get the same result. If it isn't what you want then can look at doing the options you've requested (i.e. New table etc with results) . Once again I don't know exact reason or who is accessing the reports so just going on an assumption and what access levels they will have.

So as you have setup a scheduled SQL Server Job Agent for 8PM you can add a new operator which sits under SQL Server Agent (e.g. Name: ReorganiseJobResults & for the Emailname: jsmith@testemail.com)

Once you have set this up you can then simply right click on the SQL Job itself (Reorganize) go to Properties, Notifications, check the email box, select the operator "ReorganiseJobResults" and choose for an email when the Job Fails, Succeeds or completes. An email will get set with the results.


The other thing mentioned by the previous user for Sysjobhistory is found in the msdb database. you can do something like the below code just need to modify it to your needs.


SELECT
hist.Instance_id,
hist.run_date,
job.name,
hist.run_status
FROM
[msdb].[dbo].[sysjobhistory] hist
INNER JOIN
[msdb].[dbo].[sysjobs] job
ON
hist.job_id = job.job_id
WHERE
job.name = 'Reorganize'
AND
step_id = 0
AND
hist.run_status = 1
AND
hist.run_date = 20121120
ORDER BY
hist.Instance_id DESC





With the row count is this a once off after the job has run? have you thought about making a simple SSRS report that the user can run adhoc when required or sent to the user at certain time of the day via subscription? however If you want to do the insert to a new table you would have to just do


INSERT INTO [schema].[tablename]
(
column1,
column2,
n...
)

SELECT
column1
column2
n....
FROM
sourcetable
WHERE
condition1 = ??? etc...




then once its in that table then you can do your count?

there probably are multiple ways and better ways to do it but this is just one of many... people will prob disagree with this approach.
satish._dba
satish._dba
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 168
Hi Tava,

Thanks for the reply

I too thought about the adding mails to operators but senoir collegue dont want this ( bacause if we do for this one they may ask for all other jobs, thats his concern ).

We dont use SSRS on our envirnment.

I will try ur suggestions on test env ,

other thoughts are really helpful.

Thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search