小切手番号と小切手の金額を記載した送金情報を顧客に送信しようとしています。小切手番号、金額、送金情報を記載した電子メールを顧客ごとに 1 通送信できません。
現在、電子メールが生成されますが、1 人の顧客のみを選択し、プルされたレコードの数に等しい電子メールを送信しています。以下のクエリを見てください。おそらく、カーソル部分を台無しにしています。
新しいコード
Declare @body nvarchar(max)
declare @docnum nvarchar(50)
declare @numatcard nvarchar(50)
declare @checknum nvarchar(50)
declare @checkAmt nvarchar(100)
declare @EMaIL nvarchar(200)
declare @Date nvarchar(200)
declare @table nvarchar(max)
declare @message nvarchar(Max)
declare @cardcode nvarchar(200)
Declare EmailCursor Cursor for
Select t5.checknum,T5.CheckSum,T3.E_Mail
from SAP_PROD.[dbo].[OVPM]T0 INNER JOIN SAP_PROD.[dbo].[VPM2] T1 ON T0.DocNum = T1.DocNum
left join SAP_PROD.[dbo].VPM1 T5 on T0.DocEntry=T5.docnum
left JOIN SAP_PROD.[dbo].OPCH T2 ON T1.DocEntry = T2.DocEntry And T1.InvType=T2.ObjType
left JOIN SAP_PROD.[dbo].ORPC T4 ON T1.DocEntry = T4.DocEntry And T1.InvType=T4.ObjType
left JOIN SAP_PROD.[dbo].ORIN T6 ON T1.DocEntry = T6.DocEntry And T1.InvType=T6.ObjType
left join SAP_PROD.[dbo].OCRD T3 on T3.CardCode=T0.CardCode
where T0.[DocDate]=CAST(getdate()-13 as date)
Open EmailCursor
Fetch next from EmailCursor into @checknum, @checkAmt, @Email
While (@@FETCH_STATUS=0)
Begin
set @body='<table>'
Select @body=@body+'<tr><td>'+T0.docnum+'</td>'+t0.cardcode+'</td></tr>'
from SAP_PROD.[dbo].[OVPM]T0 INNER JOIN SAP_PROD.[dbo].[VPM2] T1 ON T0.DocNum = T1.DocNum
left join SAP_PROD.[dbo].VPM1 T5 on T0.DocEntry=T5.docnum
left JOIN SAP_PROD.[dbo].OPCH T2 ON T1.DocEntry = T2.DocEntry And T1.InvType=T2.ObjType
left JOIN SAP_PROD.[dbo].ORPC T4 ON T1.DocEntry = T4.DocEntry And T1.InvType=T4.ObjType
left JOIN SAP_PROD.[dbo].ORIN T6 ON T1.DocEntry = T6.DocEntry And T1.InvType=T6.ObjType
left join SAP_PROD.[dbo].OCRD T3 on T3.CardCode=T0.CardCode
where
T5.checknum=@checknum
Set @body=@body+'</table>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'abc',
@recipients=@email,
@subject = 'Remittance',
@body = @body,
@body_format = 'HTML'
FETCH NEXT FROM EmailCursor INTO @checknum,@checkAmt,@EMAIL
END
Close EmailCursor
Deallocate EmailCursor
*古いコード***
-------------------------------------------------------------------------------------
Declare @body nvarchar(max)
Declare EmailCursor Cursor for
Select distinct t5.checknum,T5.CheckSum,T3.E_Mail
from
SAP_PROD.[dbo].[OVPM]T0 INNER JOIN
SAP_PROD.[dbo].[VPM2] T1 ON T0.DocNum = T1.DocNum
left join
SAP_PROD.[dbo].VPM1 T5 on T0.DocEntry=T5.docnum
--left JOIN
SAP_PROD.[dbo].OPCH T2 ON T1.DocEntry = T2.DocEntry And T1.InvType=T2.ObjType
--left JOIN
SAP_PROD.[dbo].ORPC T4 ON T1.DocEntry = T4.DocEntry And T1.InvType=T4.ObjType
--left JOIN
SAP_PROD.[dbo].ORIN T6 ON T1.DocEntry = T6.DocEntry And T1.InvType=T6.ObjType
left join
SAP_PROD.[dbo].OCRD T3 on T3.CardCode=T0.CardCode
where T0.[DocDate]=CAST(getdate()-9 as date)
Open EmailCursor
declare @docnum nvarchar(50)
declare @numatcard nvarchar(50)
declare @checknum nvarchar(50)
declare @checkAmt nvarchar(100)
declare @EMaIL nvarchar(200)
declare @Date nvarchar(200)
declare @table nvarchar(max)
declare @message nvarchar(Max)
declare @cardcode nvarchar(50)
Fetch next from EmailCursor into @Checknum,@checkamt,@email
While(@@FETCH_STATUS=0)
Begin
select
@docnum = t0.docnum,
@numatcard = t2.numatcard,
@checknum = t5.checknum,
@Checkamt= T5.CheckSum,
@EMAIL= T3.E_Mail ,
@Date=Convert(Nvarchar(12),T0.Docdate,101) ,
@Cardcode=T0.[CardName]
from
SAP_PROD.[dbo].[OVPM]T0 INNER JOIN SAP_PROD.[dbo].[VPM2] T1 ON T0.DocNum = T1.DocNum
left join SAP_PROD.[dbo].VPM1 T5 on T0.DocEntry=T5.docnum
inner JOIN
SAP_PROD.[dbo].OPCH T2 ON T1.DocEntry = T2.DocEntry And T1.InvType=T2.ObjType
left JOIN
SAP_PROD.[dbo].ORPC T4 ON T1.DocEntry = T4.DocEntry And T1.InvType=T4.ObjType
left JOIN
SAP_PROD.[dbo].ORIN T6 ON T1.DocEntry = T6.DocEntry And T1.InvType=T6.ObjType
left join
SAP_PROD.[dbo].OCRD T3 on T3.CardCode=T0.CardCode
where T5.checknum=@checknum
set @message ='<th><td>'+@cardcode +'Dear Vendor,</th></td></br>
Check num : ' + @checknum + ' in the amount of ' + @checkAmt+ ' has been processed by Teq and will be mailed within 48 hours. Please see remittance information below.</br>
If you have any questions or concerns regarding this payment please contact a member of our Accounts Payable staff.</br></br>
Regards,</br>
</br>
Accounts Payable Department</br>
</br>
</br>
</p>'
set @table =
N'<H1 title="T" align=center>Remittance Info</H1>' + N'<table border="2" bordercolor=#F5FFFA bgcolor=#566D7E align=center>'+
N'<table><tr><th>Docnum</th><th>numcard</th><th>checknum<th>CheckAmt</th><th>CheckDate</th><th>EMAIL</th></tr>' +
'<tr><td>' + @docnum + '</td>' +
'<td>' + @numatcard + '</td>' +
'<td>' + @checknum + '</td>'+
'<td>'+ @checkAmt + '</td>'+
'<td>'+@Date + '</td>'+
'<td>'+ @EMail +
'<td>'+ @Cardcode +'</td></tr></table>'
Set @body='<table><tr><th>'+@message+@table+'</td></tr></table>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'abc',
@recipients=@email,
@subject = 'Remittance',
@body = @body,
@body_format = 'HTML'
FETCH NEXT FROM EmailCursor INTO @checknum,@checkAmt,@EMAIL
END
Close EmailCursor
Deallocate EmailCursor