如何发送基于电子邮件的组

时间:2015-08-03 04:10:07

标签: sql sql-server email rdbms

如何发送基于电子邮件的群组?
我有三个数据

第1组= email1@xxmail.com = 2选择项目
第2组= email2@xxmail.com = 1选择项目

我使用以下方法,第一组我得到2个项目
但在两人一组我得到3件

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE dbo.procedure2
AS
BEGIN

DECLARE @tableHTML nvarchar(max)
DECLARE @Table NVARCHAR(MAX) = N''

DECLARE @tmp_list_mail table
(
  member_id INT,
  email VARCHAR(30) 
) 
insert @tmp_list_mail(member_id,email)
select c.member_id, c.email from [members]c 
inner join maintenance d on c.member_id =d.member_id
group by  c.member_id, c.email


SET NOCOUNT ON
DECLARE @member_id int, @email varchar(30)
DECLARE Contacts CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
SELECT member_id, email,25,15 FROM @tmp_list_mail;
OPEN Contacts;
FETCH NEXT FROM Contacts INTO @member_id, @email
  WHILE (@@FETCH_STATUS=0)
  BEGIN
     SELECT @Table = @Table +'<tr style="background-color:'+CASE WHEN (ROW_NUMBER() OVER (ORDER BY a.maintenance_id))%2 =1 THEN '#DCDCDC' ELSE '#F8F8FF' END +';">' +
                      '<td>' + CONVERT(VARCHAR(11), ROW_NUMBER() OVER(ORDER BY a.maintenance_id ASC))  + '</td>' +
                      '<td>' + b.vehicle_number+ '</td>' +
                      '<td>' +CONVERT(VARCHAR(11),a.tax,106)+ '</td>' +
                      '</tr>'
                      from maintenance a   
                      left join vehicles b on a.vehicle_id=b.vehicle_id  
                      left join [members] c on a.member_id =c.member_id     
                      where  c.member_id =@member_id and
                      DATEDIFF(DAY, getdate(),a.tax) =25 

                      ORDER BY a.member_id asc

                      SET @tableHTML = 
                      N'<H3><font color="Red">Maintenance List '+@email+'  </H3>' +
                      N'<table border="1" align="center" cellpadding="2" cellspacing="0" style="color:#000000;font-family:arial,helvetica,sans-serif;text-align:center;" >' +
                      N'<tr style ="font-size: 14px;font-weight: normal;background: #64950;">
                      <th>No.</th>
                      <th>Truck Number</th>
                      <th>Pajak</th>
                      </tr>' + @Table + N'</table>'   
 EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'Windows Live Mail'
      , @recipients =    @email
      , @subject = 'Maintenance Alert' 
      , @body = @tableHTML
      , @body_format = 'HTML' 
    END                    
FETCH NEXT FROM Contacts INTO @member_id, @email
CLOSE Contacts;
DEALLOCATE Contacts;
SET NOCOUNT OFF
END

1 个答案:

答案 0 :(得分:1)

很简单。您需要重置..... FETCH NEXT FROM Contacts INTO @member_id, @email WHILE (@@FETCH_STATUS=0) BEGIN SET @Table = '' -- Add this line SELECT @Table = @Table + ...... 变量,如下所示:

@Override
public void run() {
    try {

        String urlParameters = "v=oHg5SJYRHA0";
        byte[] postData = urlParameters.getBytes();
        int postLenght = postData.length;
        //url is: new URL("http://192.168.25.99:8008/apps/YouTube")
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setInstanceFollowRedirects(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestProperty("Content-Length", Integer.toString(postLenght));
        urlConnection.setUseCaches(false);
        DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
        wr.write(postData);
        wr.flush();
        wr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}