使用sql mail格式化html表

时间:2012-06-21 14:45:20

标签: html sql sql-server

我目前正在使用存储过程在表格中发送包含用户发票的电子邮件。我遇到的问题是,当发票行数达到一定数量时,它将不再打印。例如 - 我们有一个供应商有50张发票,但只有37张打印。有没有办法让表扩展到那个以外?谢谢你的帮助!

以下是存储过程中当前使用的代码:

USE [CMSOPEN]
GO
/****** Object:  StoredProcedure [dbo].[_HM_EXPENSE_EMAIL]    Script Date: 06/21/2012 10:15:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[_HM_EXPENSE_EMAIL] (@schedule_num int = 0)
AS

If @schedule_num = 0 return -1

select a.source_tran_uno, v.vendor_id, n.name remit_name, 
    IsNull(n.internet_addr, 'ap@honigman.com') internet_addr, p.car_phone, 
    addr.address1, addr.city, addr.state_code,
    c.check_num, c.check_date, c.request_num,
    i.invoice_num, i.invoice_date,
    c.chk_amt, a.ap_amt * a.sign * -1 as invoice_amt, isnull(left(t.txt1, 500), '') as comment
into #t1
from apt_invoice_amt a inner join apt_check c on a.source_Tran_uno = c.tran_uno
inner join hbm_address addr on c.address_uno = addr.address_uno
inner join apt_invoice i on a.inv_tran_uno = i.tran_uno
inner join apm_vendor v on a.remit_vend_uno = v.vendor_uno
inner join hbm_name n on v.name_uno = n.name_uno
inner join apt_text t on i.text_id = t.text_id
inner join HBM_NAME_PEOPLE p on n.PEOPLE_NAME_UNO = p.name_uno
WHERE c.SCHEDULE_NUM = @Schedule_num
and i.aptype_code = 'EXPREM'

update #t1
set internet_addr = 'ap@honigman.com'
where internet_addr = ''


SET NOCOUNT ON

Declare @HTML varchar(6000), @table VARCHAR(MAX), @source_tran_uno int, @check_date datetime, @chk_amt money, @remit_name varchar(120), 
    @vendor_id varchar(16), @city varchar(30), @state_code varchar(2), @internet_addr varchar(80),@sec_email varchar(80), @request_num int


DECLARE ach_cursor CURSOR FOR 
select distinct source_tran_uno, check_date, chk_amt, remit_name, rtrim(vendor_id), city, state_code, internet_addr, car_phone, request_num
from #t1 t
order by remit_name
OPEN ach_cursor

FETCH NEXT FROM ach_cursor 
INTO @source_tran_uno, @check_date, @chk_amt, @remit_name, @vendor_id, @city, @state_code, @internet_addr, @sec_email, @request_num

WHILE @@FETCH_STATUS = 0
BEGIN

Select @HTML =  '<H1>Expense Report Payment Advice</H1>' +
                '<BR>Date: ' + convert(varchar(10), IsNull(@check_Date, ''), 101) + '<BR>' +
                '<B>Total: $' + Cast(@Chk_Amt as varchar(15)) + '</B><BR><BR>' +
                IsNull(@remit_name, '') + '<B>(' + IsNull(@Vendor_ID, '') + ')</B><BR>' +
                '<BR><BR>' +
                'Payment for the following expense report(s) have been included as part of your payroll deposit <BR><BR>' 

SET @table =

    '<TABLE border=1 cellpadding=5>' + 
    '<tr><th width=100 align=center><B>Expenditure Date</B></th><th width=100 align=center><B>Expense Processing Date</B></th><th width=100 align=center><B>Amount Paid</B></th><th width=250 align=center><B>Reimbursement Narrative</B></th></tr>' +
CAST ( (
select td = invoice_num, '',
td = convert(varchar(10), invoice_date, 101), '',
td = '$' + convert(varchar(12), invoice_amt, 1), '',
td = comment 
from #t1 t
where t.source_Tran_uno = @source_tran_uno
              FOR XML PATH('tr'), TYPE 
    ) AS NVARCHAR(MAX) ) +
    N'</table>' ;
--select @internet_addr
--select @HTML 
--select @table
set @HTML = @HTML + @table
--select @html
-- Email delimiter must be semicolon
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Accounting ACH Notification', @recipients = @internet_addr, @copy_recipients = @sec_email, @blind_copy_recipients = 'AP@honigman.com',  

--EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Accounting ACH Notification', @recipients='AP@honigman.com', @blind_copy_recipients = 'mmurawski@honigman.com',
    @subject = 'Payment Advice from Honigman Miller' ,
    @body = @HTML,
    @body_format = 'HTML' ;

-- Get the next Billing Attorney.
FETCH NEXT FROM ACH_cursor 
INTO @source_tran_uno, @check_date, @chk_amt, @remit_name, @vendor_id, @city, @state_code, @internet_addr, @sec_email, @request_num
END 
CLOSE ACH_cursor
DEALLOCATE ACH_cursor

drop table #t1

1 个答案:

答案 0 :(得分:0)

在宣言中:

@HTML varchar(6000)

足够吗?