惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
H
Hacker News: Front Page
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
T
Tenable Blog
G
Google Developers Blog
A
About on SuperTechFans
The Cloudflare Blog
S
Securelist
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
Cisco Blogs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
Forbes - Security
Forbes - Security
腾讯CDC
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
IT之家
IT之家
博客园_首页
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
月光博客
月光博客
NISL@THU
NISL@THU
爱范儿
爱范儿
S
Secure Thoughts
K
Kaspersky official blog
Security Latest
Security Latest
T
Tailwind CSS Blog
博客园 - Franky
D
Darknet – Hacking Tools, Hacker News & Cyber Security
TaoSecurity Blog
TaoSecurity Blog
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
H
Help Net Security
T
Tor Project blog
L
LINUX DO - 热门话题
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
O
OpenAI News
S
Schneier on Security

博客园 - 光脚码农

CentOS安装JDK CentOS 7中安装和配置Promethues - 光脚码农 - 博客园 查看和指定SpringBoot内嵌Tomcat的版本 - 光脚码农 - 博客园 CentOS中安装Azkaban 2.5 Centos7 安装Nodejs SpringBoot实用技巧札记 SQL实用札记【SQL Sever篇】 话说静态构造函数 如何利用正则表达式匹配花括号内的内容 如何为Windows Forms应用程序添加启动参数(Start-Up Parameters) 利用存储过程来重命名SQL Server数据库 .NET批量操作窗口样式 ViewData、ViewBag、TempData、Session的区别与联系 如何为自己的网页实现一个“回到顶部”的链接? 如何获得数据库中所有用户创建的索引 如何为一个类型为Color的属性设置默认值 DataGridView如何绑定DataRow对象集合 Const vs. Readonly Windows下Git的安装与配置(Cygwin)
用BCP从SQL Server 数据库中导出Excel文件
光脚码农 · 2013-09-05 · via 博客园 - 光脚码农

这个程序的主要功能是从数据库中查询Job中指定step的执行信息,并将结果输出到Excel文件中,并利用SQL Server的邮件功能,发送生成的excel文件给指定的人.

DECLARE @command varchar(8000),
        @msgBody varchar (4000),
        @withJobOutcome VARCHAR(1),
        @APJobExecTime DATETIME,
        @msgSubject varchar(200),
        @reportName varchar(200),
        @rundate DATE

-- Set report date parameter
-- Set @withJobOutcome as 'T' to retrieve history of job step 'Job Outcome' as well.
SELECT @rundate = '2013-7-22', @withJobOutcome = 'F'

-- This file name is specific to DB23, please provide an available
-- path respect to the server you want to monitor.
SET @reportName = 'E:\Report\JobExecutionHistory_' + CONVERT(CHAR(10), @rundate, 120) + '.xls'

-- Remove possible report file
SELECT @command = 'del ' + @reportName
EXEC master..xp_cmdshell @command, NO_OUTPUT

-- Get execution date time of Job A0000SQ-AP
SELECT TOP 1 @APJobExecTime = CONVERT(CHAR(10), CAST(STR(h.run_date,8, 0) AS DATETIME), 120) +
       CAST(STUFF(STUFF(RIGHT('000000' + CAST(h.run_time AS VARCHAR(6)),6),5,0,':'),3,0,':') AS DATETIME)
  FROM sysjobhistory h
 INNER JOIN sysjobs j
    ON j.job_id = h.job_id
 WHERE CAST(STR(h.run_date,8, 0) AS DATE)= @rundate
   AND j.name = 'A0000SQ-AP'

-- Build Job Execution History Query Statement
SELECT @command ='bcp " SELECT ''Job Name'' JobName, ''Step Name'' StepName, '' Execution Time'' RunDate, ''Step Duration'' StepDuration, ''Execution Status'' ExecutionStatus, ''Shift'' Shift UNION ALL SELECT JobName, StepName, CONVERT(CHAR(19), RunDate, 120), StepDuration, ExecutionStatus,(CASE WHEN RunDate < ''' + CONVERT(char(19), @APJobExecTime, 120) + ''' THEN ''NA'' WHEN RunDate >= ''' + CONVERT(char(19), @APJobExecTime, 120) + ''' THEN ''AP'' END) AS Shift FROM (SELECT j.name JobName, h.step_name StepName, CONVERT(CHAR(10), CAST(STR(h.run_date,8, 0) AS DATETIME), 120) + CAST(STUFF(STUFF(RIGHT(''000000'' + CAST(h.run_time AS VARCHAR(6)),6),5,0,'':''),3,0,'':'') AS DATETIME) RunDate, LEFT(RIGHT(''000000'' + CAST(h.run_duration AS VARCHAR(10)),6),2) + '':'' + SUBSTRING(RIGHT(''000000'' + CAST(h.run_duration AS VARCHAR(10)),6),3,2) + '''''''' + RIGHT(RIGHT(''000000'' + CAST(h.run_duration AS VARCHAR(10)),6),2) + '''''''''''' StepDuration, (CASE h.run_status WHEN 0 THEN ''failed'' WHEN 1 THEN ''Succeded'' WHEN 2 THEN ''Retry'' WHEN 3 THEN ''Cancelled'' WHEN 4 THEN ''In Progress'' END) AS ExecutionStatus, h.message MessageGenerated FROM msdb..sysjobhistory h INNER JOIN msdb..sysjobs j ON j.job_id = h.job_id WHERE CAST(STR(h.run_date,8, 0) AS DATE)= ''' + CONVERT(CHAR(10), @rundate, 120) + ''' AND h.step_name not like (SELECT TOP 1 CASE ''' + @withJobOutcome + ''' WHEN ''T'' THEN '''' WHEN ''F'' THEN ''(Job outcome)'' END FROM msdb..sysjobs) AND (j.name in (''A0000SQ-NA'',''A0000SQ-AP'') or (j.name like ''A0%'' and j.name not like ''%-%''))) job ORDER BY RunDate, JobName, StepName" queryout ' + @reportName + ' -c -T -S ' + @@SERVERNAME

-- Export to Excel file
EXEC master..xp_cmdshell @command

-- Send email with the report as attachement
SET @msgBody = 'Job Execution Time history report on ' + CONVERT(CHAR(10), @rundate, 120) + ' is ready, please look detail information by checking the attached report file.'
SET @msgSubject = 'Job Execution History Report for [' + CONVERT(CHAR(10), @rundate, 120) + ']'
EXEC msdb..sp_send_dbmail @profile_name = 'Notifications',
                          @recipients = 'xxxxx@gmail.com',
                          @subject = @msgSubject,
                          @body = @msgBody,
                          @body_format ='TEXT',
                          @file_attachments = @reportName

-- Remove the temporary file after email sent
SELECT @command = 'del ' + @reportName
EXEC master..xp_cmdshell @command, NO_OUTPUT