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

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

有帮助吗?

解决方案

afaik,没有一种方法可以从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

另一个选项是通过.NET或PowerShell来利用SMO来生成脚本。 SMO参考将是:

SQLMail类

更新:

这是用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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top