PROCEDURE:
USE [Testes]
GO
/****** Object: StoredProcedure [dbo].[uspEnviarEmail] Script Date: 28/10/2015 11:05:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[uspEnviarEmail]
AS
BEGIN
DECLARE @_profileName VARCHAR(10)
DECLARE @_recipients VARCHAR(30)
DECLARE @_body VARCHAR(500)
DECLARE @_subject VARCHAR(20)
DECLARE @Contador AS INT = 0
DECLARE CursorEmails CURSOR FOR
SELECT
profileName,
recipients,
body,
subjectEmail
FROM
Emails
OPEN CursorEmails
FETCH NEXT FROM CursorEmails INTO @_profileName,@_recipients,@_body,@_subject
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Contador = @Contador +1
SET @_profileName=
(
SELECT profileName FROM Emails
)
SET @_recipients=
(
STUFF((SELECT';'+RTRIM(LTRIM(recipients)) FROM Emails FOR XML PATH('')),1,1,'')
)
SET @_body=
(
SELECT body FROM Emails
)
SET @_subject=
(
SELECT subjectEmail FROM Emails
)
EXEC msdb.dbo.sp_send_dbmail
@profile_name = @_profileName,
@recipients = @_recipients,
@body = @_body,
@subject = @_subject
END
FETCH NEXT FROM CursorEmails INTO @_profileName,@_recipients,@_body,@_subject
END