SQL代理以文本文件格式输出作业

时间:2016-12-13 17:00:18

标签: sql sql-server tsql sql-server-2012 sql-agent-job

我正在尝试将SQL Agent Job的输出写入text或excel或CSV文件。

但是它正在写它是非常巨大的文件,有白色空格和'-------'这样的线条,见下图。我尝试修剪,删除特殊字符。

这是我的工作。

public class ACAStatusAdjustmentCollectionValidator 
   : BaseValidator<ItemCollection<IEmployeeACAStatus>>
{
    public ACAStatusAdjustmentCollectionValidator(ISecureReadContext isolatedContext) : base(isolatedContext)
    {
        this.RuleForEach(x => x.Items).Must(this.SomeValidation)
        //This when is based off of the collection items instead of the item itself
        //.When(x => x.);
    }
}

这里是屏幕截图或整个受害者和阻止查询被空格分隔的文件。

以下是一些屏幕截图,就像它写入文件时的样子一样。这是锁定发生时的结果,我想将其写入文本文件,但它会写入数千个空格。见下图。

enter image description here

实际文本文件, enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

将输出手动格式化为单个字符串以避免这种情况。例如:

Select cast(data.BlockingSessionID as varchar(max)) + '||' + 
    cast(data.VictimSessionID as varchar(max) + '||' + 
    LTRIM(RTRIM(data.BlockingQuery)) + '||' + 
    LTRIM(RTRIM(data.VictimQuery)) + '||' + 
    CAST(data.WaitDurationSecond as varchar(max)) + '||' + 
    data.WaitType + '||' +
    Cast(data.BlockingQueryCompletePercent as varchar(max))
from(
SELECT
     blocking_session_id AS BlockingSessionID,
     session_id AS VictimSessionID,




     (SELECT [text] FROM sys.sysprocesses
      CROSS APPLY sys.dm_exec_sql_text([sql_handle])
      WHERE spid = blocking_session_id) AS BlockingQuery,

     [text] AS VictimQuery,
     wait_time/1000 AS WaitDurationSecond,
     wait_type AS WaitType,
     percent_complete AS BlockingQueryCompletePercent
FROM sys.dm_exec_requests
CROSS APPLY sys.dm_exec_sql_text([sql_handle])
WHERE blocking_session_id > 0) data