导出数据发送邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导出数据发送邮件相关的知识,希望对你有一定的参考价值。
use TEST
go
create PROCEDURE [dbo].[SP_产品百分之三价格]
AS
BEGIN
SET NOCOUNT ON;
/*1.************************************************/
select
Convert(varchar(8),GETDATE(),112) as 日期
,a.主件品号
,a.主件品名
,a.当前采购总价
,a.上次采购总价
,a.比率
,‘ChengPin‘ as 类型
,‘‘ as 备注
into ##A
from
(
select
a.主件品号
,a.主件品名
,sum(a.当前采购总价) as 当前采购总价
,sum(a.上次采购总价) as 上次采购总价
,(sum(a.当前采购总价)-SUM(a.上次采购总价))/SUM(a.上次采购总价) as 比率
from
(
select
a.主件品号
,c.MB002 as 主件品名
,a.元件品号
,b.MB002 as 元件品名
,a.实际用量
,b.UDF62 as 当前单位采购价
,b.UDF62 * a.实际用量 as 当前采购总价
,b.UDF61 as 上次单位采购价
,b.UDF61 * a.实际用量 as 上次采购总价
,b.UDF60 as 比率
,a.单位
,a.属性
,a.备注
from MB as a
left join INVMB as b on a.元件品号=b.MB001
left join INVMB as c on a.主件品号=c.MB001
where
a.属性 = ‘采购件‘
) as a
where a.比率 > 0.03 or a.比率 < -0.03
GROUP BY a.主件品号,a.主件品名
) as a
/*2.************************************************/
if exists(select 1 from ##A)
begin
declare @XUHAO int
select ROW_NUMBER() OVER (ORDER BY @XUHAO ASC) AS ID, a.* into ##B from ##A as a
FULL OUTER JOIN
(select * from MA where MA07 = ‘ChengPin‘) as b
on b.MA02 = a.主件品号 and b.MA04 = a.当前采购总价 and b.MA05 = a.上次采购总价
where MA02 IS NULL
--select * from ##B
/*3.************************************************/
if exists(select 1 from ##B)
begin
delete MA where MA07 = ‘ChengPin‘
insert into MA (MA01,MA02,MA03,MA04,MA05,MA06,MA07,MA08) select * from ##A
update MA set MA08 = ‘Y‘ where MA02 in(select 主件品号 from ##B )
/*4.************************************************/
--delete MA where MA07 = ‘ChengPin‘ and MA02 like ‘JC%‘
DECLARE @Columns VARCHAR(max) = ‘‘
,@Data NVARCHAR(max)=‘‘
,@File NVARCHAR(max) = Convert(varchar(8),GETDATE(),112) + ‘产品比率.xls‘
,@PathFile NVARCHAR(max)
set @PathFile = ‘D:ExportFile‘ + @File
SELECT @Columns = @Columns + ‘,‘‘‘ + name +‘‘‘‘
,@Data = @Data + ‘,Convert(Nvarchar,[‘ + name +‘])‘
FROM tempdb.sys.columns WHERE object_id = OBJECT_ID(‘tempdb..##B‘)
set @Columns = ‘SELECT ‘+ SUBSTRING(@Columns,2,LEN(@Columns))
set @Data = ‘select ‘+ SUBSTRING(@Data,2,LEN(@Data)) + ‘ from ##B‘
DECLARE @cmd NVARCHAR(4000) = ‘bcp "‘
+ @Columns
+ ‘ Union All ‘
+ @Data
+‘" queryout ‘
+ @PathFile + ‘ -c -q -T‘
EXEC master..xp_cmdshell @cmd
/*5.************************************************/
exec msdb.dbo.sp_send_dbmail
@profile_name = ‘Fly_Huang‘,
@recipients = ‘[email protected];[email protected];‘,
@subject = @File,
@body = ‘Test01‘,
@body_format = ‘html‘ ,
@file_attachments= @PathFile
/*6.*************************************************/
DROP TABLE ##B
end
end
DROP TABLE ##A
/*
select * from MA
delete MA where MA07 = ‘ChengPin‘ and MA02 like ‘120%‘
DROP TABLE #A
*/
end
以上是关于导出数据发送邮件的主要内容,如果未能解决你的问题,请参考以下文章