我有一个作业调度程序,假设根据 20 个不同的部门向 20 个不同的 id 发送每日收集报告。每个部门将仅收到该特定部门的 1 份报告。这是我提出的问题。
DECLARE @xml NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)
SET @xml = CAST((select tm.name as 'td','',h.name as 'td','',h.account_number AS 'td','',
SUM(bc.total_amount) AS 'td'
FROM MJP.dbo.tbl_bank_collection bc,
MJP.dbo.tbl_div_type_master tm,
MJP.dbo.tbl_div_header h
where bc.type_id = tm.id
and bc.header_id = h.id
and bc.transaction_date = '06-12-2012'
and bc.div_id in ( select d.id
from tbl_div d, tbl_bank_collection bc
where bc.div_id = d.id
group by d.id)
group by tm.name, h.name,h.account_number with rollup
having grouping(h.name) = 1 or
GROUPING(h.account_number) = 0
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
SET @body ='<html><i>Collection Report</i>
<body bgcolor=red><table border = 1><tr><th>Type Name
<th>Header Name</th><th>Account Number</th>
<th>Total Amount</th></tr>'
SET @body = @body + @xml +'</table></body></html>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name='alkesh mail',
@body_format ='HTML',
@recipients='id.no1@yahoo.com;id.no2@yahoo.com',
@subject='Daily Report',
@body=@body
----------------------------------------------------------------------------------------
现在我想在计算特定部门的最终总金额后拆分报告,并且应该为下一个部门的 id 生成下一个报告。
任何建议或澄清!