473,378 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Sending formated Email with database mail sql 2005

1
Hi,
My objective is to send out invoice statements with database mail by means of a stored procedure.
There are two tables, Table1 is customer information, Table2 is invoice information.
Table 1 has Account Number, Customer Email, etc..
Table2 has Account Number, Invoice Number, etc..
This is one of my failed attempts trying to use Cursors.
Any suggestion on how I can accomplish this.
Thank you.

declare @msg varchar(2000),
@subject varchar(30),
@statedate varchar(50),
@accountno varchar(50),
@amountdue nchar(10),
@companyname varchar(50),
@stateaddress varchar(50),
@statecity varchar(50),
@st varchar(50),
@statezip nchar(10),
@comment varchar(50),
@statecurrent varchar(50),
@day3160 int,
@day6190 int,
@over90 int,
@fc money,
@email varchar(50),
@AccountNo1 varchar(50),
@InvoiceDate varchar(50),
@InvoiceNo varchar(50),
@Rt varchar(50),
@CustomerNo varchar(50),
@Charges varchar(50),
@Credits varchar(50),
@Balance varchar(50)


declare email_cur CURSOR
for

select StateDate, AccountNo, AmountDue,
companyName, StateAddress, StateCity,
St, StateZip, Comment,
StateCurrent, day3160, day6190,
over90, FinanceCharge, Email
from Table1
open email_cur
fetch next from email_cur into
@statedate,
@accountno,
@amountdue,
@companyname,
@stateaddress,
@statecity,
@st,
@statezip,
@comment,
@statecurrent,
@day3160,
@day6190,
@over90,
@fc,
@email

while (@@Fetch_status=0)
begin
declare email_cur1 CURSOR
for
select AccountNo, InvoiceDate,
InvoiceNo, Rt, CustomerNo,
Charges, Credits, Balance
from Table2
where accountno = @accountno
open email_cur1
fetch next from email_cur1 into
@AccountNo1,
@InvoiceDate,
@InvoiceNo,
@Rt,
@CustomerNo,
@Charges,
@Credits,
@Balance
while (@@Fetch_status=0)
begin
fetch next from email_cur1 into
@AccountNo1,
@InvoiceDate,
@InvoiceNo,
@Rt,
@CustomerNo,
@Charges,
@Credits,
@Balance
end
deallocate email_cur1

set @msg= ' html stuff e.g.
<tr>
<td width="17%">' + @InvoiceDate + '</td>
<td width="16%">' + @InvoiceNo + '</td>
<td width="7%">' + @Rt + '</td>
<td width="11%">' + @CustomerNo + '</td>
<td width="15%">' + @Charges + '</td>
<td width="17%">' + @Credits + '</td>
</tr>'

exec msdb.dbo.sp_send_dbmail
@profile_name = 'emailer',
@recipients=@email, @body=@msg,
@subject='Invoice Statement',
@body_format = 'HTML'

fetch next from email_cur into
@statedate,
@accountno,
@amountdue,
@companyname,
@stateaddress,
@statecity,
@st,
@statezip,
@comment,
@statecurrent,
@day3160,
@day6190,
@over90,
@fc,
@email

end
deallocate email_cur
Jun 29 '07 #1
0 1555

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: praba kar | last post by:
Dear All, I have doubt regarding mail sending smtplib module. The below code is I used to send a mail. ########################################## import email.Message import email.Utils...
5
by: BaWork | last post by:
I have a web form where a client can select which site members to send an email to. This form is populated from the contents of the member table, so the form can have 0-x names listed on it...
5
by: Brian Hanson | last post by:
Hi, I have an asp.net (vb) app where I am creating a Crystal Report, exporting it to a .pdf file and then e-mailing that report, as an attachment, to several users using...
6
by: Eduardo Rosa | last post by:
Somebody knows how I queue email using .Net? thanks a lot
3
by: Sydney | last post by:
Hi, I am trying to construct a WSE 2.0 security SOAP request in VBScript on an HTML page to send off to a webservice. I think I've almost got it but I'm having an issue generating the nonce...
2
by: HK | last post by:
In VB.NET, I'm getting the exception "failure sending mail". I'm running VS 2005 on XP Home. This is a new install on a new PC. I've never had email problems with VS 2003, and there I could...
5
by: Jai | last post by:
Hi, I am in a problem of sending mass emails(newsletter) to my website members. Actually my problem is this: I want to send newsletter to my website members. But I had given a facility for...
0
by: krouxsa | last post by:
Hi There people... Please help me with this query. I want to send 1 email to multiple email addresses stored in a database. Will it be possible? I'm using SQL Server 2005 and already set up...
9
by: JoeP | last post by:
Hi All, How can I find the reason for such an error: Failure sending mail. Some Code... oMailMessage.IsBodyHtml = False oMailMessage.Body = cEmailBody Dim oSMTP As New SmtpClient...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.