SQL Agent Jobs, how to document - sql

I want to end up with a sort of Calendar that shows the different scheduled jobs and their steps (the exact command executed). But I"m having a hard time finding all the data. So my question is two-fold:
1) Does anyone know of a software (preferably open source or free) that can get this data from sql server 2000 in a spreadsheet or other easy to manipulate format?
2) Can anyone help me with the queries I'd need to get this data. How are SQL Agent Jobs organized in the database?
Any guidance on this issue would be welcome.

The information regarding jobs is stored in the MSDB database on your server.
Tables in the MSDB database will be of interest to you (names are self-explanatory)
sysjobs
sysjobsteps
The schedules are stored here
sysjobschedules
You can get execution history here
sysjobhistory
There are several built in procedures to help you with the details:
sp_help_job
sp_help_jobhistory
sp_help_jobschedule

Using this query to get the info I need:
SELECT j.name, database_name,Step_id, Subsystem, command, Sjs.enabled, SJS.freq_type,
sjs.freq_interval, sjs.freq_subday_type, freq_subday_interval, freq_recurrence_factor, next_run_date, next_run_time
FROM sysjobsteps SJ
INNER JOIN sysjobs J ON SJ.Job_id = J.Job_Id
INNER JOIN sysJobschedules SJS ON J.Job_ID = SJS.Job_id
order by J.name, SJ.step_id

Related

Find SQL Server Job Related to Existing SSIS Package in Visual Studio

I've started working in the middle of an existing project and I want to identify the Job in SSMS which is running an existing package in visual studio (SSIS). Is there a way to do that or only searching job by job?
The project has several jobs so I was wondering if there's an easier way to identify the job that is running a specific SSIS package.
Thank you in advance,
The following script will return all SQL server jobs, and the packages that they run. If your package store isn't SSIDB\Live you will need to change this value for your own environment.
select
j.name AS job
, s.step_name as step
, s.command
, SUBSTRING((SUBSTRING(s.command,(CHARINDEX('SSISDB\Live\',s.command)+
LEN('SSISDB\Live\')),len(s.command))),0,(CHARINDEX('.dtsx',(SUBSTRING(s.command,(CHARINDEX('SSISDB\Live\',s.command)+LEN('SSISDB\Live\')),len(s.command)))))+5) as package
from
msdb.dbo.sysjobsteps s
inner join msdb.dbo.sysjobs j
on s.job_id = j.job_id and s.subsystem ='SSIS'

Multiple server SQL query in .Net

I have a query that currently runs successfully on SQL Server Management Studio, gathering information from 2 databases on the SQL server where I run the query, and one database on a linked SQL Server. The query has this structure:
SELECT TOP 10
DB1.id_number as BLN,
DB1.field1,
DB2.field2,
DB3.field3
FROM
LinkedServer.database1.dbo.table1 DB1
INNER JOIN
database2.dbo.table2 DB2 ON DB1.id_number = DB2.id_number
INNER JOIN
database3.dbo.table3 DB3 ON DB3.id_number2 = DB1.id_number2
WHERE
DB1.id_number IS NOT NULL
AND DB1.field1 IS NOT NULL
How can I run this same query from a .Net application? I am looking for a solution that doesn't require saving a View on the database.
In whatever solution you propose, please detail connection strings and security issues.
Thank you.
You can run the query using SqlCommand. Although doing it with an ORM may be a little tricky, if it even can be done.

SQL Server - user permissions/securables change report

Is there a default report in Sql Server that will output the permissions/securables history for given user or login? If not, does anyone know how to craft such a query? We had an incident recently where a user mysteriously lost insert permissions on a table, and we'd like to find out exactly what caused it.
The only way to figure this out is to read transaction log that stores details on each transaction. If your database was in full recovery mode then the info is somewhere in there.
Unfortunately you can’t do this using standard tools because MS doesn’t support this.
You can either get yourself a commercial log reader or try to hack this using undocumented commands like fn_dblog.
Check these out for more details
Read the log file (*.LDF) in sql server 2008
SQL Server Transaction Log Explorer/Analyzer
How to view transaction logs in sql server 2008
Here is a query for finding the permissions on a database. We have been using a variant of this query to copy permissions from one table to another table.
select *
FROM sys.database_permissions AS dp
INNER JOIN sys.objects AS o ON dp.major_id=o.object_id
INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id
INNER JOIN sys.database_principals AS dpr ON dp.grantee_principal_id=dpr.principal_id

SQL Server 2008R2: How to find what query/procedure a sql job agent is running?

I have a mssql database with some sql job agents for backing up the database. This was done by someone before me and i need to find the script that is being run by the job agent, so i can find out where the database backups are being stored. is there any way i can find this script or procedure that the job agent is executing ? Just to let you know i am not a dba and i have very little knowledge of the db management, so please try to explain as if your explaining to a newbie.
Since the UI is pretty clunky, I suggest learning where these are stored in the metadata:
SELECT s.step_id, s.database_name, s.command
FROM msdb.dbo.sysjobsteps AS s
INNER JOIN msdb.dbo.sysjobs AS j
ON s.job_id = j.job_id
WHERE j.name = 'your job name goes here';
You could look at multiple jobs by changing the WHERE clause, you could also do pattern matching against s.command to find steps that reference certain objects...
In the SQL Server Agent at the bottom of the Object Explorer, you'll find the Jobs folder. The scripts etc. will be inside these jobs. You can use the Job Activity Monitor to see if any jobs are currently running.

SQL Server 2000 Script to list all Meta Data Services DTS packages

I am doing an SSIS package that would go out to all our DB servers to gather information about them, and what is on them. And in this one case there is a SQL 2000 box with a bunch of DTS packages on there.
Now I can easily list the Local Packages using "exec sp_enum_dtspackages", but this doesn't list the Meta Data Services Packages.
I cannot find anything obvious online or in the database to help me with this, not really even then attaching a profiler to it.
So I was hoping someone might be able to help me with a script. I am looking for the name, description, owner and the create_date.
Thanks in advance.
use msdb
go
select no.Name, si.Comments as Description, vai.CreateByUser as Owner, vai.VersionCreateTime as CreateDate
from RTblNamedObj no
inner join RtblSumInfo si
on no.IntID = si.IntID
inner join rtblVersionAdminInfo vai
on no.IntID = vai.IntID