Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1032 Part VI Enterprise Data Management FIGURE 43-17 Create a new profile and add accounts to it. FIGURE 43-18 Create a new account for Database Mail. 1032 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1033 Automating Database Maintenance with SQL Server Agent 43 7. Notice that the new account you created in the previous step is added to the profile, as shown in Figure 43-19. Clicking Add sets up multiple accounts for the Database Mail profile to allow for Failover and avoid single points of failure. FIGURE 43-19 Add an account to the Database Mail profile. 8. Click Next to proceed. This brings up the Manage Profile Security dialog, shown in Figure 43-20. Here you can specify the database users or roles that can access the pro- files. The public profile allows all users to access the profile, whereas the private profile allows only specific users to access the profile. This dialog also enables you to specify whether the profile is a default public profile. A default public profile allows you to send e-mail without explicitly specifying the profile. 9. Click Next to bring up the Configure System Parameters dialog, shown in Figure 43-21. Based on your requirements, you can change the Database Mail system parameters in this dialog. You may want to change the retry attempts to 3 instead of the default value of 1. 10. Click Next to review the configuration settings you have made. This brings up the Complete the Wizard dialog, shown in Figure 43-22. 11. Click Finish. This will bring up the Configuring dialog, shown in Figure 43-23. 1033 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1034 Part VI Enterprise Data Management FIGURE 43-20 You can specify which users have access to the profile and specify the default profile. FIGURE 43-21 The Database Mail system parameters can be changed. 1034 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1035 Automating Database Maintenance with SQL Server Agent 43 FIGURE 43-22 Review the configuration settings that you have made. FIGURE 43-23 Underlying stored procedures are being run to configure Database Mail. 1035 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1036 Part VI Enterprise Data Management Congratulations. Database Mail is now configured successfully and can be used in a manner similar to SQL Mail. Although the Database Mail Configuration Wizard makes it simple to configure Database Mail, it may not be the fastest way to configure Database Mail on multiple SQL Servers. The following T-SQL script can be used to perform the exact same steps that were achieved with the Database Mail Configura- tion Wizard: Enable Database Mail sp_configure ‘show advanced options’, 1; GO RECONFIGURE; GO sp_configure ‘Database Mail XPs’, 1; GO RECONFIGURE GO Create a Database Mail profile EXECUTE msdb.dbo.sysmail_add_profile_sp @profile_name = ‘SQL2008_DBMail_Profile’, @description = ‘SQL Server 2008 Database Mail Profile’ ; Create a Database Mail account EXECUTE msdb.dbo.sysmail_add_account_sp @account_name = ‘uttam_parui’, @description = ‘Database Mail Account’, @email_address = ‘uttam_parui@domain_name.com’, @display_name = ‘Uttam Parui’, @replyto_address = ‘uttam_parui@domain_name.com’, @mailserver_name = ‘smtpserver.domain_name.com’ ; Add the account to the profile EXECUTE msdb.dbo.sysmail_add_profileaccount_sp @profile_name = ‘SQL2008_DBMail_Profile’, @account_name = ‘uttam_parui’, @sequence_number =1 ; Grant permission for a user or a role to use the Database Mail profile EXECUTE msdb.dbo.sysmail_add_principalprofile_sp @profile_name = ‘SQL2008_DBMail_Profile’, @principal_id = 0, @is_default = 1 ; Now Database Mail is ready to send e-mails. To do so, use the sp_send_dbmail stored procedure. This stored procedure accepts a lot of parameters, most of which are optional. The following T-SQL code can be used to send a simple text e-mail: EXECUTE msdb.dbo.sp_send_dbmail @profile_name=’SQL2008_DBMail_Profile’, @recipients=’pauln@sqlserverbible.com’, 1036 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1037 Automating Database Maintenance with SQL Server Agent 43 @subject=’Database Mail Test’, @body=’This is a test e-mail sent from Database Mail on SQL2008\INST1’ To send e-mail using the Database Mail feature, you must be a member of either the sysadmin fixed server role or the DatabaseMailUserRole in msdb. To view the status of the e-mails, use the sysmail_allitems view as follows: SELECT * FROM msdb.dbo.sysmail_allitems; One of the important columns in this view is sent_status. It contains one of the following four val- ues: sent, unsent, retrying,orfailed. If your e-mails are not going out, check this view to ver- ify that the e-mails are being queued up. If you notice messages with anything other than sent status, right-click the Database Mail folder in Management Studio and select View Database Mail Log from the context menu to view detailed error messages. Best Practice A ll the e-mails and attachments are stored in msdb internal tables. These tables are not maintained automatically and based on your e-mail activity, they may become very big. It is recommended that you periodically use the system stored procedures msdb.dbo.sysmail_delete_mailitems_sp and msdb.dbo.sysmail_delete_log_sp to maintain the msdb internal tables. You can use the steps learned in this chapter to configure a SQL Server Agent job to run these stored procedures automatically. Summary SQL Server Agent is a powerful ally that will ensure you never forget to perform a crucial maintenance task, and that will alert you when something critical requires your attention. The former goal is achieved through recurring jobs, the latter through alerts. This chapter covered how to set up SQL Server Agent. You learned what alerts, operators, and jobs are, and the steps required to manage them. Finally, you learned how to configure Database Mail so that the SQL Server Agent service can send e-mail and page notifications when alerts occur. In short, you should now be fully equipped to use all features of SQL Server Agent to automate crucial maintenance tasks. 1037 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1038 www.getcoolebook.com Nielsen c44.tex V4 - 07/21/2009 2:34pm Page 1039 Transferring Databases IN THIS CHAPTER Using the Copy Database Wizard Generating SQL scripts Detaching and attaching databases Using the Import and Export Wizard T ransferring data may be a mundane task, but SQL Server databases are often developed on one server and deployed on other servers. Without a reliable and efficient method of moving database schemas and whole databases, the project won’t get very far. SQL Server enables multiple means of moving databases. As a database developer or database administrator (DBA), you should have basic skills in the following topics, three of which are covered in this chapter: ■ Copy Database Wizard ■ SQL scripts ■ Detach/Attach ■ Backup/Restore (covered in Chapter 41, ‘‘Recovery Planning’’) The keys to determining the best way to move a database are knowing how much of it needs to be moved and whether or not the servers are directly connected by a fast network. Table 44-1 lists the copy requirements and the various methods of moving a database. Copy Database Wizard The Copy Database Wizard actually generates a SQL Server Integration Services (SSIS) package that can copy or move one or more databases from one server to another. If the database is being moved to a server on the same network server, this is the premiere method. This method won’t work to copy a database from SQL Server 2008 to an older version of SQL Server. In addition, both source and 1039 www.getcoolebook.com Nielsen c44.tex V4 - 07/21/2009 2:34pm Page 1040 Part VI Enterprise Data Management What’s New in SQL Server Configuration? sp_attach_db is a deprecated feature and will be removed in future versions of SQL Server. It is recommended to use CREATE DATABASE database_name FOR ATTACH instead. The SQL Server Import and Export Wizard has additional options for d ata type conversions that are required by import and export operations. For example, it provides visual cues of the data type mapping for each table or view, which helps to proactively check for errors. Also, the wizard can now be started from Start ➪ Programs ➪ Microsoft SQL Server 2008. TABLE 44-1 Database Transfer Methods Requirement Copy Database Wizard SQL Scripts Detaching Attaching Backup Restore Exclusive Access to the Database Yes No Yes No Copies Between Disconnected Servers No Yes Yes Yes Copies Database Schema Yes Yes Yes Yes Copies Data Yes Depends on the script Yes Yes Copies Security Server logins, database users, security roles, and permissions Depends on the script Database users, security roles, and permissions Database users, security roles, and permissions Copies Jobs/ User-Defined Error Messages Yes Depends on the script Yes Yes destination servers must have the SQL Server Agent running (it i s stopped by default when working with SQL Server 2008). The Copy Database Wizard offers the most flexibility a nd capability. Its only limitation is that it requires exclusive access to the database. You access the Copy Database Wizard by right-clicking the database you want to copy and choosing Tasks ➪ Copy Database from the context menu. Skip past the Welcome to the Copy Database Wizard page by clicking Next. 1040 www.getcoolebook.com Nielsen c44.tex V4 - 07/21/2009 2:34pm Page 1041 Transferring Databases 44 For more information about starting and stopping SQL Server Agent, refer to Chapter 4, ‘‘Installing SQL Server 2008.’’ On pages 1 (Select a Source Server) and 2 (Select a Destination Server), as shown in Figure 44-1 and 44-2, respectively, the Copy Database Wizard begins by gathering the name of the source and destination servers and the required security information to log into the server. FIGURE 44-1 Select the source server On page 3 (Select the Transfer Method), shown in Figure 44-3, the wizard asks how you want to transfer the database. Using the detach and attach method is faster, but it requires that SQL Server have additional rights to both the source and destination databases, and you must allow exclusive access to both. The detach and attach method works best for large databases. The SQL Management Object (SMO) method doesn’t require any special access and users can continue using the source database. However, this method is significantly slower and is not recommended for large databases. On page 4 (Select Databases), shown in Figure 44-4, the wizard enables you to select the databases you want to move or copy. The status column indicates whether it is OK to move the database; if it isn’t, an explanation is provided. For example, system databases ( master, msdb, model,andtempdb) cannot be moved or copied. 1041 www.getcoolebook.com . about starting and stopping SQL Server Agent, refer to Chapter 4, ‘‘Installing SQL Server 2008. ’’ On pages 1 (Select a Source Server) and 2 (Select a Destination Server) , as shown in Figure 44-1 and. Messages Yes Depends on the script Yes Yes destination servers must have the SQL Server Agent running (it i s stopped by default when working with SQL Server 2008) . The Copy Database Wizard offers the most. copy a database from SQL Server 2008 to an older version of SQL Server. In addition, both source and 1039 www.getcoolebook.com Nielsen c44.tex V4 - 07/21/2009 2:34pm Page 1040 Part VI Enterprise