SQL Server代理TSQL作业错误:将查询结果导出为CSV并通过电子邮件发送CSV

时间:2017-03-20 17:08:54

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

以下TSQL代码因此错误而失败:“作为用户执行:。无法初始化sqlcmd库,错误号为-2147467259。[SQLSTATE 42000](错误22050)。步骤失败。”

我不确定为什么,任何帮助都将不胜感激!在没有我必须添加的分隔内容之前,我测试了同样的查询。 (本文中的XML事物和包装SELECT命令https://marc.durdin.net/2015/07/concatenating-strings-in-sql-server-or-undefined-behaviour-by-design/

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'IMGEMAIL',
@recipients = '~~~email~~~',
@subject = 'Sunday',
@query = N'
SET nocount ON;
DECLARE @data VARCHAR(MAX)
DECLARE @delim VARCHAR(10) = '', ''

SELECT @data = (
SELECT
c.firstName AS FirstName + @delim
,c.lastName AS LastName + @delim
,c.userID AS CandidateID + @delim
,c.city AS CandidateCity + @delim
,c.state AS CandidateProvince + @delim
,u.primaryDepartment AS IMGDepartmentID + @delim
,d.name AS IMGDepartment + @delim
,js.dateadded AS JobSubDateAdded + @delim
,js.status AS JobSubStatus + @delim
,wh.companyName AS WorkHistoryCompanyName + @delim
,wh.startDATE AS WorkHistoryStartDate + @delim
,wh.endDATE as WorkhistoryEndDate + @delim
,wh.title AS WorkHistoryTitle
FROM 
[IanMartin_Datamart].[dbo].[JobSubmission] AS js
INNER JOIN
[IanMartin_Datamart].[dbo].[CandidateWorkHistory] AS wh ON    js.candidateID = wh.candidateID
INNER JOIN
[IanMartin_Datamart].[dbo].[Candidate] AS c ON c.userID = wh.candidateID
INNER JOIN
[IanMartin_Datamart].[dbo].[CorporateUser] AS u ON u.userID = js.sendingUserID
INNER JOIN
[IanMartin_Datamart].[dbo].[CorporationDepartment] AS d ON d.departmentID = u.primaryDepartment
WHERE
js.dateadded >= DATEADD(day,-7, GETDATE())
AND
js.status = ''Client Submission''
AND
u.primaryDepartment IN (28822, 28823, 28824)
AND
wh.startDATE >= ''2012-01-01''
ORDER BY 
js.candidateID
FOR XML PATH(''''), TYPE
).value(''.'',''varchar(max)'');
PRINT @Summary;',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'bhLeads.csv',
@query_result_separator = ',',
@query_result_no_padding = 1

0 个答案:

没有答案