码迷,mamicode.com
首页 > 其他好文 > 详细

导出数据发送邮件

时间:2018-07-28 12:03:20      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:count   类型   mat   object_id   create   union   pat   chm   tempdb   

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 = ‘rd4@topledlighting.com;mc@topledlighting.com;‘, @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

导出数据发送邮件

标签:count   类型   mat   object_id   create   union   pat   chm   tempdb   

原文地址:http://blog.51cto.com/gongxi/2151349

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!