gpt4 book ai didi

sql - 遍历 XML 节点并在 SQL Server 中发送电子邮件

转载 作者:数据小太阳 更新时间:2023-10-29 02:29:08 26 4
gpt4 key购买 nike

我正在获取存储过程的 XML,我需要对其进行迭代并根据 XML 发送电子邮件。

我有另一个用于发送电子邮件的存储过程:

MailingSystem..SP_SendEmail 
@Cc = N'', -- nvarchar(500)
@Bcc = N'', -- nvarchar(500)
@FromEMailAddress = N'', -- nvarchar(500)
@ReplyToEMailAddress = N'', -- nvarchar(500)
@Subject = N'', -- nvarchar(500)
@EmailRecipient = N'', -- nvarchar(500)
@ContentType = N'', -- nvarchar(50)
@Content = N'', -- nvarchar(max)

这是 XML 的结构:

DECLARE @XMLData XML =          
'<GenericXML>
<GenericEmailsNotification>
<EmailMessage>
<From>John@Google.com</From>
<Recipients>Naomi@Yahoo.com</Recipients>
<CC>Tania@HotMail.com</CC>
<Subject>Follow The Sun </Subject>
<Body>Breathe, breathe in the air. Set your intentions.Dream with care. </Body>
</EmailMessage>
<EmailMessage>
<From>ron@Google.com</From>
<Recipients>Fake@Yahoo.com</Recipients>
<Subject>Second Email is best</Subject>
<Body>Second Email body...</Body>
</EmailMessage>
</GenericEmailsNotification>
</GenericXML>'

我想遍历每个 EmailMessage 节点并提取与该节点/电子邮件相关的数据(主题、发件人、收件人、正文等)并将其发送到负责的存储过程发送邮件。

每个 XML 可以有多个电子邮件要发送(如果到现在还不清楚..)

我该怎么做?有什么方法可以遍历 XML 元素?

提前致谢

最佳答案

您可以解析 XML 并使用游标:

DECLARE @XMLData XML =          
'<GenericXML>
<GenericEmailsNotification>
<EmailMessage>
<From>John@Google.com</From>
<Recipients>Naomi@Yahoo.com</Recipients>
<CC>Tania@HotMail.com</CC>
<Subject>Follow The Sun </Subject>
<Body>Breathe,breathe in the air.Set your intentions.Dream with care</Body>
</EmailMessage>
<EmailMessage>
<From>ron@Google.com</From>
<Recipients>Fake@Yahoo.com</Recipients>
<Subject>Second Email is best</Subject>
<Body>Second Email body...</Body>
</EmailMessage>
</GenericEmailsNotification>
</GenericXML>' ;

查询:

SELECT  [from]       = s.c.value('(./From)[1]', 'nvarchar(250)')
,[Recipients] = s.c.value('(./Recipients)[1]', 'nvarchar(250)')
,[CC] = s.c.value('(./CC)[1]', 'nvarchar(250)')
,[Subject] = s.c.value('(./Subject)[1]', 'nvarchar(250)')
,[body] = s.c.value('(./Body)[1]', 'nvarchar(MAX)')
INTO #Emails
FROM @XMLData.nodes('/GenericXML/GenericEmailsNotification/EmailMessage')
AS s(c);

DECLARE cur CURSOR LOCAL FAST_FORWARD FOR
SELECT [from],[Recipients],[CC],[Subject],[body] FROM #Emails;

DECLARE @from NVARCHAR(250),
@recipients NVARCHAR(250),
@cc NVARCHAR(250),
@subject NVARCHAR(250),
@body NVARCHAR(MAX);

OPEN cur;
FETCH NEXT FROM cur INTO @from, @recipients, @cc, @subject, @body;

WHILE @@FETCH_STATUS = 0
BEGIN
-- send email, pass variables to SP call, handle `NULL` with `ISNULL` if needed
-- EXEC MailingSystem..SP_SendEmail @Subject = @subject, ...

SELECT 'Send email in cursor loop:', @from, @recipients, @cc, @subject, @body;

FETCH NEXT FROM cur INTO @from, @recipients, @cc, @subject, @body;
END

CLOSE cur;
DEALLOCATE cur;

LiveDemo

关于sql - 遍历 XML 节点并在 SQL Server 中发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36532548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com