Solved

Updating statistics with error notification email in SQL server

Posted on 2016-10-20
4
46 Views
Last Modified: 2016-10-22
First of all, I'm sorry I am not SQL expert, but I have to do this tomorrow, so I need a quick starting code to do what I want.
There are 2000 tables I need to update statistics, but last time, it fails in the middle of updating statistics and I had to start from the begining. I like to break down the 2000 tables to small  pieces like 200 tables and want to get a notification email if it failed or no. Then, I can fix and continue from the point where it stopped.

When I generated codes for the Maintenance Plan for updating statistics, it gives like this;
UPDATE STATISTICS [dbo].[tbZones_CRC] 
WITH FULLSCAN,COLUMNS
GO
UPDATE STATISTICS [dbo].[TNIRErrorMessages] 
WITH FULLSCAN,COLUMNS
GO
UPDATE STATISTICS [dbo].[TNIRInBox] 
WITH FULLSCAN,COLUMNS
GO
UPDATE STATISTICS [dbo].[TNIROutBox] 
WITH FULLSCAN,COLUMNS
GO
UPDATE STATISTICS [dbo].[TNIRPricingResults] 
WITH FULLSCAN,COLUMNS
GO
UPDATE STATISTICS [dbo].[TRC04030601] 
WITH FULLSCAN,COLUMNS
GO

Open in new window


Quesitons:
1.As far as I understand this seems it does in sequential orders which is good for my purpose.  
I have 6000 lines of this code, like to use for or while loop to read the table name and do update statistics, then when it fails and finished successfully, I like to receive an email. Can someone give me a basic code to do this?

The Database email is set up on the server.

2.Can you give me codes to show what tables failed for updating statistics so that I can run manually in the next morning?
0
Comment
Question by:crcsupport
  • 3
4 Comments
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41852907
Dont do it this way but instead use Ola Hallegren maintenance solution the IndexOptimisation stored procedure. It is the best there is. It will not fail due to a single step but it will just log it and continue the process until completed. Here it is:

https://ola.hallengren.com/

Just follow the instruction there and you will be just fine. You can break the jobs into multiple ones if you want. I recommend you to also run the index optimization part as it is very important for performance but it will take lot longer if they have not been optimized for a long time. If uyou want only th estatistics this is how you do it:

EXECUTE dbo.IndexOptimize
@Databases = 'USER_DATABASES',
@FragmentationLow = NULL,
@FragmentationMedium = NULL,
@FragmentationHigh = NULL,
@UpdateStatistics = 'ALL'

This will apply on all the user databases on the server. If you want only a particula database then you just give it the name:

EXECUTE dbo.IndexOptimize
@Databases = 'YourDatabase',
@FragmentationLow = NULL,
@FragmentationMedium = NULL,
@FragmentationHigh = NULL,
@UpdateStatistics = 'ALL'
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41852913
The best way to execute this is to use the Job that is created for you and just update the stored procedure call with the one that applies to you. Be aware that is uses not he SQL query to execute it but the sqlcmd utility called from the job for the exact reason I mentioned above, to not stop on one database. This is how the step command will look like in your case:

sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d zb_dba_maint -Q "EXECUTE dbo.IndexOptimize @Databases = 'YourDatabase', @FragmentationLow = NULL, @FragmentationMedium = NULL, @FragmentationHigh = NULL, @UpdateStatistics = 'ALL', @OnlyModifiedStatistics='Y'" -b

I added @OnlyModifiedStatistics='Y' so it will ignore "not moving" parts.
0
 
LVL 26

Accepted Solution

by:
Zberteoc earned 500 total points
ID: 41852928
Actually if you are only do this for one database then it is simpler to execute the stored procedure:
EXECUTE dbo.IndexOptimize 
	@Databases = 'YourDatabase', 
	@FragmentationLow = NULL, 
	@FragmentationMedium = NULL, 
	@FragmentationHigh = NULL, 
	@UpdateStatistics = 'ALL', 
	@OnlyModifiedStatistics='Y'
	
	-- if you want just to see the statements that will execute without actually executing them(for test/check purpose) uncomment the line below
	--,@Execute='N'

Open in new window

You can use the @Execute='N' parameter to just check the statements without executing them.
0
 
LVL 1

Author Comment

by:crcsupport
ID: 41852936
Thanks for the good script.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

743 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

11 Experts available now in Live!

Get 1:1 Help Now