Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Updating statistics with error notification email in SQL server

Posted on 2016-10-20
4
108 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

792 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