SSIS Package to generate MailProfile script

SSIS Package to generate MailProfile script.

There is no provision to generate script for mail profile in sql server. but i have one script that can be used to create mail profile, mail account and map them together.

I am using this script whenever i needed it, letter i have an idea to start scripting automatically of mail profile and i have created one package that genrates all three scripts and store it in one sql file. this is very helpfull for disaster recovery planning for script backup.

Step1 : To create package you need one Data Flow Task, One Flat File Connection, One OLEDB Connection, and one variable
Please see in figure.

BackupMailProfileScript 1

Step2 : Drag one OLE DB Source and one Flat File Destination. Right click on Data Flow Task –> Edit

BackupMailProfileScript 2

Step3 : Right click on OLEDB Source Editor — > Edit

Select your local machine connection.

Select SQL Command in Data Access mode.

Paste Below Code.


Select '--Create Account' AS CMD
Union All
SELECT 'EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = ''' + m.name + ''',
@description = ''' + m.description + ''',
@email_address = ''' + m.email_address + ''',
@replyto_address = ''' + m.replyto_address + ''',
@display_name = ''' + m.display_name + ''',
@mailserver_name = ''' + s.servername + ''',
@mailserver_type = ''' + s.servertype + ''',
@port = ''' + cast(s.port as nvarchar) + ''',
@username = ''' + isnull(c.credential_identity,0) + ''',
@password = ''x'',
@use_default_credentials = 0,
@enable_ssl = 0' AS CMD
From msdb.dbo.sysmail_account m
LEFT OUTER JOIN msdb.dbo.sysmail_server s
ON m.account_id = s.account_id
LEFT OUTER JOIN master.sys.credentials c
ON s.credential_id = c.credential_id
Union All
Select '--Create Profile' AS CMD
Union All
SELECT '
EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = ''' + name + ''',
@description = ''' + isnull(description,'NULL')+ '''
' AS CMD
from msdb.dbo.sysmail_profile
Union All
Select '--Create Link For Profile to Account' AS CMD
Union All
Select '
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = '''+ a.name +''',
@account_name = ''' + b.name + ''',
@sequence_number = 1 ' as CMD
from msdb.dbo.sysmail_profile as a
join msdb.dbo.sysmail_account as b on a.name = b.name

BackupMailProfileScript 3

Step4 : Click on Columns left side on OLE DB Source editor, configure column and click ok.

BackupMailProfileScript 4

Step5 : Right Click on Flat File Destination and select “Primary_Location” to flat file connection manager.

BackupMailProfileScript 5

Click on Mappings to map variable.

BackupMailProfileScript 6

Step6 : Now to make folder dynamic – select Primary_location from connection manager and hit F4.

BackupMailProfileScript 7

Click on Expression button.

BackupMailProfileScript 8
Copy below code to Expression.

@[User::FolderName]  + "<a href="file://mailprofile/">\\MailProfile</a>_" +  (DT_WSTR,4)YEAR(GETDATE())
+ RIGHT("0" + (DT_WSTR,2)MONTH(GETDATE()), 2)
+ RIGHT("0" + (DT_WSTR,2)DAY( GETDATE()), 2) + ".sql"

Click OK and execute the package it will creates script to “D:\ScriptBackup\MailProfile\MailProfile_YYYYMMDD.Sql” this file contains three different bunch of script. Fist to create Mail Account Second to create Mail Profile. Third to map Mail Account to Mail Profile.

Regards,

Nirav Gajjar

SSIS Package with Dynamic ADO Connection

SSIS Package with Dynamic ADO Connection.

I have created a package that collects database information from different instances all over the network and stored in one central database table.

For this, I have collected Server Name, Database Name, Logical File Name, Physical File Name and File Size in MB.

I have stored all instance info in my central database table.

Table Script.

Create Table SQLServerInstances
(

    ServerName Varchar(20),
    DBName Varchar(20),

    UserName Varchar(20),

    PWD Varchar(20)
)
Insert Into SQLServerInstances Values('yourLocalMachineName','AdventureWorks2012','sa','password')

 I had taken one Execute SQL Task to get list of instances then move it to Foreach loop and store the values to database table using Data Flow Task.

Step1 : Drag Execute SQL Task, Foreach Loop Container and Data Flow task under Foreach Loop Container.

DynamicConnection 1

Step2 : Create Variables.

–> Obj_Result : DataType Object.

–> ServerName : DataType String.

–> DBName : DataType String.

–> UserName1 : DataType String.

–> PWD : DataType String.

DynamicConnection 2

Step3 : Right click on Execute SQL Task –> Edit and fill the details as shown in below figure.

DynamicConnection 3

Step4 : Select Result Set on left pane of Execute SQL Task and select your object variable.

DynamicConnection 4

Step5 : Right Click on Foreach Loop Container –> Edit.

Select Foreach ADO Enumerator in Enumerator and Select Obj_Result in ADO Object source varialbe field.

DynamicConnection 5

Step6 : Select Variable Mapping on left pane of Foreach Loop Container and select varialbes as you see in below figure.

DynamicConnection 6

Step7 : Now Right click on Data Flow Task –> Edit.

DynamicConnection 7

Step8 : Right click on OLE DB source. create one OLEDB data connection with name Dynamic Connection with your local database connection.

SQL Command.


SELECT @@ServerName as ServerName,
   DB_NAME(database_id) AS DatabaseName,
   Name AS LogicalName ,
   Physical_Name as PhysicalName,
   (size*8)/1024 DBSize
FROM sys.master_files

DynamicConnection 8

Step9 : Select Columns from left pane of OLE DB Source Editor and confirm Columns.

DynamicConnection 9

Step10 : Right Click on Data Conversion Task –> Edit and set data types for columns.

DynamicConnection 10

Step11 : Create one database table to store all servers’s database info in central database.


Create Table DatabaseInfo
(
    ServerName Varchar(20),
    DBName varchar(20),
    LogicalName Varchar(200),
    PhysicalName Varchar(200),
    DBSize int
)

DynamicConnection 11

Step12 : Right click on mapping on left pane of OLE DB Destination Editor and map the varialbes to table columns.

DynamicConnection 12

Step13 : Now right click on “DynamicConnection” that you have created before to make it dynamic.

Rikght click on it and click on Property.

Select Expression field button that will open one popup screen.

DynamicConnection 13

Map DynamicConnection’s parameter with varialbes to make it dynamic and run the project.

Regards,

Nirav gajjar