Ch 10. Maintaining and Automating SQL Server Requires SQL SP1
Check Database Check database To check and repair database DBCC CHECKDB To check and repair database Set to single user mode first: ALTER DATABASE AdventureWorks SET SINGLE_USER WITH ROLLBACK IMMEDIATE DBCC CHECKDB (‘AdventureWorks’, REPAIR_REBUILD) Set back to mult-user mode ALTER DATABASE AdventureWorks SET MULTI_USER WITH ROLLBACK IMMEDIATE
Shrink Database When data is deleted, space is not released until you shrink database To shrink database: Right click on a database Select tasks->shrink->database Check Reorganize files… Click ok
Automation Automation uses SQL Server Agent to run and schedule Works on T-SQL codes, scripting languages, operating system command, replication tasks Contain Alerts , operators, and jobs
Automation - Operator An operator is someone who is to be contacted in case of problems occur in SQL Server Can be contacted via email, pager, Net Send ( a pop up notification on screen) Multiple operators can be created, each respond to different problems / events
Automation- operator Example of a Net Send
Automation - Operator Create an operator Expand on SQL Server Agent, Right click on operator, select New Operator In the Name box, enter Administrator In the Net Send address box, enter your computer name Leave Pager on duty schedule empty, which will allow the operator to be contacted anytime Click OK to finish.
Automation - Operator Fail-Safe Operator – a default operator to receive notifications when no other operator is available Right click on SQL Server Agent->Properties On Alert system page, check Enable fail-safe Operator, select Administrator as the operator, Notify using Net Send Click OK to finish, Administrator is now also the fail-safe operator
Automation - Job A job is a series of tasks to be performed Assign operators to a job to receive notifications/messages
Automation - Job Create a job and assign to operator Expand SQL Server Agent Right click on Jobs and select New Job In Name box, enter TestJob In Steps page, click the new button to create a new step
Automation - Job In Step name box, type Create database Type following codes: CREATE DATABASE AutomationTest ON PRIMARY ( NAME=AutomationTest_dat, FIlENAME=‘c:\AutomationTest.mdf’, SIZE=10MB, MAXSIZE=15, FIlEGROWTH=10%)
Automation - Job Click Parse button to verify codes Click Advanced page, On Success Action = Go to next step On Failure Action = Quit Job and report failure Click OK to create the job
Automation - Job Job Scheduling On Schedule page, click New button In the name box, type Create Database Select One time from schedule type, check Enabled Set one time occurrence-> Time to 5 min after current time Click OK to continue
Automation - Job Assign an operator to a job On notification page, check Net Send, and select Administrator as operator Select When the job completes. Administrator will receive a Net Send message when the job is complete Click OK to continue.
Automation - Alert A notification to user of specific event Notification can be in form of email, pager, Net Send
Automation - Alert To create an alert Expand SQL Server Agent Right click Alerts, select New Alert In the name box, type Number Alert Select <all databases> which will cause alert to be applied to all databases Check Error Number
Automation - Alert Enter 14599 into error number text box On Response page, check Notify Operators Check Administrator->Net Send In options page, under include alert error text in , check net send Open new query, type Raiserror(14599,10,1) WITH LOG to simulate alert.
Automation – Custom Error Use Master Sp_addmessage @msgnum=50001, @severity=10,@msgtext=N’ this is a custom error. ’ , @with_log=‘TRUE’
Automation – Performance Alert Alerts an operator when a performance event is met Frequently used to detect performance problem in its early stages
Automation – Performance Alert Expand SQL Server Agent Right click Alerts, select New Alert In the name box, type Performance Alert Select <all databases> which will cause alert to be applied to all databases In type list, select SQL Server Performance Condition Alert
Automation – Performance Alert In Counter box, select Percent Log Used In Instance box, select AdventureWorks Select Alert If Counter to Falls Below In Value Box, type 100 Select Response page, Notify Administrator by Net Send Click OK
Automation – Performance Alert A alert message will pop up on screen notify Administrator Log File used is less than 100% Right-click on Performance Alert->disable to stop pop-ups
Maintenance Plan Wizard A wizard automatically create jobs for you to maintain your SQL database A good way to group maintenance jobs into one package
Maintenance Plan Wizard Expand Server->Management, Right click Maintenance Plans, and select Maintenance Plan Wizard In Select Plan Properties dialog box: In Name text box, enter Maintenance Plan Test Select Single Schedule for entire plan or no schedule Click Change to bring up job schedule properties Change Schedule type to One Time Add 10 minutes to time Click ok, then click next In Select Maintenance Tasks dialog box, check following tasks and press next: Shrink database Backup database (full) Backup database (differential) Backup database (Transaction Log) Maintenance Clean Up Tasks
Maintenance Plan Wizard In Task Order page, use default order and click next In Define Shrink Database Tasks, select all databases and click next In each Define Backup Database, select all databases and click next In Define Maintenance Cleanup Tasks, use default and click next Use default in Select Report option, click next Verify and complete the wizard by click Finish Check jobs that are created for you automatically
Copy Database Wizard A wizard to help you copy a database to another location Option to allow you to copy database online
Copy Database Wizard Right click on a database, select Tasks->copy Database to start the wizard Select your server Select destination server, for this exercise, copy to the same server Select Use SQL Management Object method ( slower)
Copy Database Wizard Select databases to copy Configure destination database Use default name for package Schedule SSIS job, use Run Immediately Click finish