2010-08-19 36 views
13

Tôi đã sử dụng giao diện SQL Server 2008 để thiết lập hồ sơ thư cơ sở dữ liệu trên máy chủ thử nghiệm của mình và bây giờ tôi muốn nhân bản chúng vào cơ sở dữ liệu sản xuất của mình.Thiết lập kịch bản thư cơ sở dữ liệu

Có cách nào để tạo tập lệnh để thực hiện việc này không?

Trả lời

20

AFAIK, không có cách nào nhất thiết phải viết kịch bản lệnh này từ SSMS nhưng bạn có thể tạo tập lệnh có thể vận chuyển trong TSQL một lần và sử dụng lại nó trên tất cả các máy chủ. Dưới đây là ví dụ tốt để bạn bắt đầu với điều này:

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 protected]', -- 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 

Tùy chọn khác sẽ là tận dụng SMO, thông qua .NET hoặc powerhell để tạo tập lệnh. Các tài liệu tham khảo SMO cho điều này sẽ là:

SqlMail Class

UPDATE:

Sau đây là cách dễ dàng bật ra được để kịch bản này với Powershell và 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(); 
+1

Jonathan - bạn cartesian tham gia/bom! – MaasSql

+1

Đây là một tìm kiếm tuyệt vời! Về phía TSQL, bạn có thể sử dụng '@@ SERVERNAME' để tham gia vào một số tham số nhất định nếu bạn muốn tài khoản được tùy chỉnh theo tên của mỗi máy chủ, tức là' donotreply_FooBarSQL @ mycompany.com'. – NateJ

Các vấn đề liên quan