Tag Archives: Select

SQL Agent Jobs Last Run

Sometimes there is a need to see last run time information for a SQL Agent job. You may need to see when the job is scheduled to run next.  The msdb system database contains a ton of information about SQL Agent jobs schedules, activity, job steps and etc..

I created the below query to get a bird’s eye view of job executions. The query is based off msdb.dbo.sysjobs and msdb.dbo.sysjobactivity. These objects have a lot more information that can be added. Tweak the results to meet your needs.

Jobrollup3

Read more »

Find Object Execution

I found a useful script to find executions of stored procedure. This will give you a good idea of what is utilization. Run it multiple times to compare how often a procedure of group of procedures executes within certain time periods.

Stored Procedure Executions

Stored Procedure Executions

For example, you have a under performing billing process. Your system fires multiple procedures multiple times. You could use something like this to get an idea of execution counts, worker time, average elapsed time and max logical reads. It will point you in the right direction procedure that can be optimized. Tweak this to fit your needs.
Read more »

Identify Lead Blocker Script

Many third-party tools have  pretty lead blocking graphs and etc. You may not have access to something than grand.  My friend and co-work JD Pellerin wrote this script. He used this as a basis for an SSRS report that can be executed by operations teams to identify potential batch issues. Play around with tweak it to fit your needs.

Blocking Results

Sample Blocking Results

Read more »

AlwaysOn Availability Groups – Query to Find Latency Part 3

Like Part 1 and Part 2, I use a similar query to find latency in AlwaysOn Availability Groups. The queries will return one line for each DB in the group. Part 1 , we looked at a simple query to grab the current status of the replica. Part 2, we looked at populating a Global Temp table every minute to gain a historical perspective of our AG latency. This query will look into creating an email notification when certain criteria is met. It will send the current status to a specific set of email recipients.

The example will be around specific criteria set as a step used in a SQL Agent job. If you use something like this, you need to tweak it to you organization’s needs. I do suggest that you set the job to a frequency that will not overflow your inbox with notices.

Send an email from server when latency for a specfic database datbase is over x number of minutes.

  •  Alter If statement > value for specific needs. This example fires of fthe email when latency is over 60 minutes.
  •  Alter “database_id = DB_ID(‘MyAGDatabase’)” to specify datbase to monitor
  • Alter “@recipients=N’[email protected];” to specify recipients. I suggest using a email distribution list over listing spefic emails.

The example is pretty simple and intended for demonstration purposes.

Read more »

AlwaysOn Availability Groups – Query to Find Latency Part 1

AlwaysOn Availability Groups latency can be a real concern. Microsoft provides a AlwaysON Dashboard to monitor and some administration of Availability Groups in SQL Server Management Studio(SSMS). Ben Snaidero as an excellent posting about the dashboard and T-SQL monitoring of AG.

The query below uses sys.dm_hadr_database_replica_states to query the current state. It can be run on the primary or secondary server. We use a readable secondary and it is nice to see how close to real-time updates. As a roll-your-own solution, you could create a SQL Agent job to monitor and send an email when certain criteria is met.

Read more »

Script to Get SQL Agent Job History

Wanted to share this script. I pieced it together some time ago from various scripts. I think this might be the source for at least some of the query.

Removing the step_ID from the where clause will show run times by step_id and Total RunTime (step_id 0).
Read more »

Query to View Active Sessions

I want to start out by saying I am not sure about the origin of this query. I use it frequently to find activity on my servers. I left some of the code commented to show a few example items you can add and additional search criteria. I really encourage anyone to grab a query and alter it to show information useful to them.

It uses sys.dm_exec_query_plan() (via inline select forgive me) to grab the query plan when possible. Additional information to grab performance information and SQL code executing uses these DMVs sys.dm_exec_requests, sys.sysprocesses and CROSS APPLY sys.dm_exec_sql_text(). Use the links to see the information being returned.

Read more »

T-SQL: Cursor Example

ARTICLE FROM ORIGINAL SITE – Young and Dumb!

Database Cursors have long been a favorite of mine. It allows you to take a subset of data and output the information in various ways. I use them for such things as building the body of an automated email with a cursor set within a stored proceedure. You can return output to build a cleaner report output, etc.Example

Select 'Case Created: '+convert(Varchar(20), @DATE ) + ' Case Number: '+ convert(varchar(10), @CaseNum)

We will start with the basic frame work and build from that:

We will use a two tables named SomeTable and Other Table
(The tables are actually from MySQL – I was lazy.)
Read more »

Script Commands to Disable/Enable SQL Agent Jobs

The script below is a quick way to create disable and enable SQL Agent job commands for existing jobs. It will script out the correct ‘EXEC msdb..sp_update_job’ statements. In this example, the where clause filters for “enabled = 1”, but you can use it create statements based of various job characteristics. We tend to use these when migrating to new servers and want to leave the old server in place.

/*
Script commands to disable/enable jobs
5/20/2014 JCD
Tested: 2005, 2008 R2 and 2012 SP1

You can further filter jobs with other sysjobs columns to specify
an even smaller subset of enabled jobs. Good examples for adding
filers are owner_sid, description and category_id.

*/
SET NOCOUNT ON
GO

USE msdb
GO
-- Script disable commands for enabled jobs
SELECT 'EXEC msdb..sp_update_job @job_name='''+REPLACE(name,'''','''''')+''',@enabled=0;'
FROM msdb..sysjobs
WHERE enabled = 1;
GO
-- Script enable commands to enabled jobs after disabling. Should be run when creating the disable script
SELECT 'EXEC msdb..sp_update_job @job_name='''+REPLACE(name,'''','''''')+''',@enabled=1;'
FROM msdb..sysjobs
WHERE enabled = 1;
GO