1
+ /* #info
2
+
3
+ # Autor
4
+ Rodrigo Ribeiro Gomes
5
+
6
+ # Descrição
7
+ Script de exemplo de como criar um profiler no database mail.
8
+ Não foi eu quem criou ele.
9
+ Provavelmente peguei de alguma interface que gera ou de alguma fonte na internet.
10
+
11
+
12
+ */
13
+
14
+
15
+ -- -----------------------------------------------------------
16
+ -- Database Mail Simple Configuration Template.
17
+ --
18
+ -- This template creates a Database Mail profile, an SMTP account and
19
+ -- associates the account to the profile.
20
+ -- The template does not grant access to the new profile for
21
+ -- any database principals. Use msdb.dbo.sysmail_add_principalprofile
22
+ -- to grant access to the new profile for users who are not
23
+ -- members of sysadmin.
24
+ -- -----------------------------------------------------------
25
+
26
+ DECLARE @profile_name sysname ,
27
+ @profile_description nvarchar (256 ),
28
+ @account_name sysname ,
29
+ @SMTP_servername sysname ,
30
+ @email_address NVARCHAR (128 ),
31
+ @display_name NVARCHAR (128 );
32
+
33
+ -- - ALTERAR OS DADOS AQUI:
34
+
35
+ -- Profile name. Replace with the name for your profile
36
+ SET @profile_name = ' DBA' ;
37
+ SET @profile_description = ' Profile default para ser usado pelos scripts de monitoramento do DBA'
38
+
39
+ -- Account information. Replace with the information for your account.
40
+
41
+ SET @account_name = ' SQL Server' ;
42
+ SET @SMTP_servername = ' Servidor SMTP' ;
43
+ SET @email_address = ' Email do Remetente' ;
44
+ SET @display_name = ' Nome do Remetente' ;
45
+
46
+
47
+
48
+
49
+
50
+ -- DAQUI PRA FRENTE, NÃO PRECISA ALTERAR NADA!!!!
51
+
52
+
53
+ -- Verify the specified account and profile do not already exist.
54
+ IF EXISTS (SELECT * FROM msdb .dbo .sysmail_profile WHERE name = @profile_name)
55
+ BEGIN
56
+ RAISERROR (' The specified Database Mail profile (%s) already exists.' , 16 , 1 , @profile_name);
57
+ GOTO done;
58
+ END ;
59
+
60
+ IF EXISTS (SELECT * FROM msdb .dbo .sysmail_account WHERE name = @account_name )
61
+ BEGIN
62
+ RAISERROR (' The specified Database Mail account (%s) already exists.' , 16 , 1 , @account_name) ;
63
+ GOTO done;
64
+ END ;
65
+
66
+ -- Start a transaction before adding the account and the profile
67
+ BEGIN TRANSACTION ;
68
+
69
+ DECLARE @rv INT ;
70
+
71
+ -- Add the account
72
+ EXECUTE @rv= msdb .dbo .sysmail_add_account_sp
73
+ @account_name = @account_name,
74
+ @email_address = @email_address,
75
+ @display_name = @display_name,
76
+ @mailserver_name = @SMTP_servername;
77
+
78
+ IF @rv<> 0
79
+ BEGIN
80
+ RAISERROR (' Failed to create the specified Database Mail account (%s): %d' , 16 , 1 ,@account_name,@rv) ;
81
+ GOTO done;
82
+ END
83
+
84
+
85
+
86
+ -- Add the profile
87
+ EXECUTE @rv= msdb .dbo .sysmail_add_profile_sp
88
+ @profile_name = @profile_name
89
+ ,@description = @profile_description
90
+ ;
91
+
92
+ IF @rv<> 0
93
+ BEGIN
94
+ RAISERROR (' Failed to create the specified Database Mail profile (%s): %d' , 16 , 1 , @rv);
95
+ ROLLBACK TRANSACTION ;
96
+ GOTO done;
97
+ END ;
98
+
99
+ -- Associate the account with the profile.
100
+ EXECUTE @rv= msdb .dbo .sysmail_add_profileaccount_sp
101
+ @profile_name = @profile_name,
102
+ @account_name = @account_name,
103
+ @sequence_number = 1 ;
104
+
105
+ IF @rv<> 0
106
+ BEGIN
107
+ RAISERROR (' Failed to associate the speficied profile (%s) with the specified account (%s): %d' , 16 , 1 , @profile_name,@account_name,@rv) ;
108
+ ROLLBACK TRANSACTION ;
109
+ GOTO done;
110
+ END ;
111
+
112
+ COMMIT TRANSACTION ;
113
+
114
+ done:
115
+
116
+ GO
0 commit comments