SQL query mail address multiple lines merge one line, then send the message

Source: Internet
Author: User
--创建测试表use FlygoIF OBJECT_ID(‘test‘) IS NOT NULL DROP TABLE testCREATE TABLE test( Id int IDENTITY(1,1) NOT NULL, Name varchar(50) NULL, Judge char(1) NULL, Mail varchar(50) NULL)GO--插入测试数据INSERT INTO test ( Name ,Judge ,Mail) SELECT ‘张三‘,‘Y‘,‘[email protected]‘ UNION ALL SELECT ‘李四‘,‘Y‘,‘[email protected]‘ UNION ALL SELECT ‘王五‘,‘N‘,‘[email protected]‘ GOSELECT * FROM test--查询多行合并DECLARE @Maliaddress varchar(8000)set @Maliaddress = ‘‘‘‘ + STUFF((SELECT ‘;‘ + Mail FROM test WHERE Judge in (‘Y‘) FOR xml path(‘‘)),1,1,‘‘) + ‘‘‘‘ select @Maliaddress as ‘MailAddress‘ exec msdb.dbo.sp_send_dbmail@profile_name = ‘Fly_Huang‘, @recipients = @Maliaddress, @subject = ‘Test电子邮件的主题‘, @body = ‘Test电子邮件的正文‘, @body_format = ‘HTML‘

SQL query mail address multiple lines merge one line, then send the message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.