0
I have a Procedure that sends email with the table data, including the email that will be forwarded,but it is sending to only the 1 record, the second it passes straight, and she is in Loop Infinity, sending several Emails to only the 1st record
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
Please check the result before publishing, there is a specific formatting for code: http://answall.com/editing-help
– brasofilo
It is SQL Server the database?
– rray