Sql server 2008 数据库邮件的脚本设置

Sql server 2008 数据库邮件的脚本设置,sql-server-2008,email,database-mail,Sql Server 2008,Email,Database Mail,我已经使用SQLServer2008GUI在我的测试服务器上设置了数据库邮件配置文件和帐户,现在我想将它们复制到我们的生产数据库中 有没有办法生成一个脚本来执行此操作?好的,没有办法从SSM中编写脚本,但是您可以在TSQL中创建一个可传输的脚本,并在所有服务器上重用它。下面是一个很好的示例,可以让您开始了解这一点: USE [master] GO sp_configure 'show advanced options',1 GO RECONFIGURE WITH OVERRIDE GO sp_c

我已经使用SQLServer2008GUI在我的测试服务器上设置了数据库邮件配置文件和帐户,现在我想将它们复制到我们的生产数据库中


有没有办法生成一个脚本来执行此操作?

好的,没有办法从SSM中编写脚本,但是您可以在TSQL中创建一个可传输的脚本,并在所有服务器上重用它。下面是一个很好的示例,可以让您开始了解这一点:

USE [master]
GO
sp_configure 'show advanced options',1
GO
RECONFIGURE WITH OVERRIDE
GO
sp_configure 'Database Mail XPs',1
GO
RECONFIGURE 
GO
-- Create a New Mail Profile for Notifications
EXECUTE msdb.dbo.sysmail_add_profile_sp
       @profile_name = 'DBA_Notifications',
       @description = 'Profile for sending Automated DBA Notifications'
GO
-- Set the New Profile as the Default
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
    @profile_name = 'DBA_Notifications',
    @principal_name = 'public',
    @is_default = 1 ;
GO
-- Create an Account for the Notifications
EXECUTE msdb.dbo.sysmail_add_account_sp
    @account_name = 'SQLMonitor',
    @description = 'Account for Automated DBA Notifications',
    @email_address = 'email@domain.com',  -- Change This
    @display_name = 'SQL Monitor',
    @mailserver_name = 'smtp.domain.com'  -- Change This
GO
-- Add the Account to the Profile
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
    @profile_name = 'DBA_Notifications',
    @account_name = 'SQLMonitor',
    @sequence_number = 1
GO
另一种选择是利用SMO,通过.NET或powershell生成脚本。SMO对此的参考是:

更新:

以下是使用Powershell和SMO编写脚本的简单程度:

[void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo");

#Set the server to script from
$ServerName = "ServerName";

#Get a server object which corresponds to the default instance
$srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server $ServerName

#Script Database Mail configuration from the server
$srv.Mail.Script();

乔纳森-你是笛卡尔加入/炸弹!这是一个了不起的发现!在TSQL端,如果您希望根据每个服务器的名称自定义帐户,则可以使用
@@SERVERNAME
来替代某些参数,即
donotreply_FooBarSQL@mycompany.com