Solved

Identify Agent jobs which target a specific DB

Posted on 2014-02-18
4
283 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:ScottPletcher
ScottPletcher 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now