Solved

Identify Agent jobs which target a specific DB

Posted on 2014-02-18
4
301 Views
Last Modified: 2014-03-09
We have a SQL Server 2012 with multiple databases and several SQL Server Agent jobs.  Is there a way for me to quickly identify jobs which contain steps affecting a specific DB?
0
Comment
Question by:canuckconsulting
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 400 total points
ID: 39869185
1.  Check all the steps where the following is true:
SELECT  *
FROM    dbo.sysjobs j
        INNER JOIN sysjobsteps s ON j.job_id = s.job_id
WHERE   s.database_name = 'YourDatabaseName'

2. Check the contents of the command column for the database name as in:
SELECT  *
FROM    dbo.sysjobs j
        INNER JOIN sysjobsteps s ON j.job_id = s.job_id
WHERE   CHARINDEX('YourDatabaseName.', command) > 0
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 100 total points
ID: 39870736
Anthony's code looks good, except:

I wouldn't include the "." after the database name, because rather than multi-part object name -- db_name.schema.table_name -- the code might contain "use db_name select ... from schema.table_name".


Also, be aware that of course a proc called from a job may reference any number of other dbs within that proc, which the above code cannot identity.

For example, say you are looking for references to dbA.

An existing job might have code that runs:
EXEC dbB.dbo.proc1
and the proc could then reference objects in dbA.

There's really not a good way to programmatically trace all that down.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39872018
Anthony's code looks good, except:
I did think of that but I wanted to avoid any false positives.  But you make a good point.
0
 

Author Closing Comment

by:canuckconsulting
ID: 39916386
Thanks guys!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Stored Proc - Rewrite 42 58
Compare a numeric value to a varchar w/o getting an error 4 20
First Max value 3 28
SQL R 21 25
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question