SQL 文本文件格式的代理作业输出
SQL Agent Job output in text file formatting
我正在尝试将 SQL 代理作业的输出写入文本或 excel 或 CSV 文件。
但它写满了它,使它成为一个非常大的文件,其中包含空格和类似“------”的行,请参见下图。我尝试修剪、删除特殊字符。
这是我的工作。
Select data.BlockingSessionID,data.VictimSessionID,LTRIM(RTRIM(data.BlockingQuery)),LTRIM(RTRIM(data.VictimQuery)),data.WaitDurationSecond,data.WaitType,data.BlockingQueryCompletePercent
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
这里是屏幕截图或文件,其中整个 Victim 和 Blocking 查询由空格分隔。
这是一些屏幕截图,因为它写入时的样子 file.This 是锁定发生时的结果,我想将其写入文本文件,但它写入了数千个空格。见下图
实际文本文件,
手动将输出格式化为单个字符串以避免这种情况。例如:
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
我正在尝试将 SQL 代理作业的输出写入文本或 excel 或 CSV 文件。
但它写满了它,使它成为一个非常大的文件,其中包含空格和类似“------”的行,请参见下图。我尝试修剪、删除特殊字符。
这是我的工作。
Select data.BlockingSessionID,data.VictimSessionID,LTRIM(RTRIM(data.BlockingQuery)),LTRIM(RTRIM(data.VictimQuery)),data.WaitDurationSecond,data.WaitType,data.BlockingQueryCompletePercent
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
这里是屏幕截图或文件,其中整个 Victim 和 Blocking 查询由空格分隔。
这是一些屏幕截图,因为它写入时的样子 file.This 是锁定发生时的结果,我想将其写入文本文件,但它写入了数千个空格。见下图
实际文本文件,
手动将输出格式化为单个字符串以避免这种情况。例如:
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