SQLServer2008导出EXCEL文件,然后使用SQL脚本发送邮件的实现


一.将表数据导出到EXCEL。
二.配置SQL SERVER2008 的>Database Mail。
三.执行邮件发送脚本。


一.将表数据导出到EXCEL。

1,如果xp_cmdshell没有开启,开启xp_cmdshell的方法
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO

2,执行导出脚本
EXEC master..xp_cmdshell 'bcp "SELECT * FROM testDB.dbo.Table_1 " queryout C:/TEMP/test.xls -c -Utest -P123' --Sservername


二.配置SQL SERVER2008 的>Database Mail。
1.Management-->Database Mail(Right Click)-->Configure Database Mail(这里需要使用默认的SMTP服务器)
2.After send test mail success.
帮助可参考:ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/3bdb0e6d-9d09-465e-9a3f-7a8ccd53aca8.htm

三.执行邮件发送脚本。

USE msdb
GO
EXEC sp_send_dbmail @profile_name='SQLMailProfileName',
@recipients='wqiu@test.com', @subject='Test message',
@body='This is the body of the test message. _
Congrats Database Mail Received By you Successfully.'
,@file_attachments = 'C:/TEMP/test.xls'
帮助可参考:ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/f1d7a795-a3fd-4043-ac4b-c781e76dab47.htm

 

对数据库邮件进行故障排除:

http://msdn.microsoft.com/zh-cn/library/ms188663.aspx

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值