美好的一天!我目前正在创建一个应该生成包含昨天声明标记的自动电子邮件的作业。我已经完成了创建作业并创建存储过程以获取将在Excel文件中的数据。当我尝试执行我创建的作业时,它会给出一个如下错误:
Msg 22051,Level 16,State 1,Line 0 附件文件C:\在MMS 20141013111305AM.xls中标记的声明无效。
我想知道这个错误发生在哪里。我尝试了其他可能的解决方案,但我仍然得到同样的错误。我还以为我可能无权访问服务器中的文件或插入文件。
这是我的工作:
DECLARE @MONTH CHAR(2)
DECLARE @DATENOW CHAR(40)
DECLARE @YEAR CHAR(4)
DECLARE @DAY CHAR(2)
DECLARE @HOUR CHAR(20)
DECLARE @MINUTES CHAR(2)
DECLARE @SECONDS CHAR(2)
DECLARE @AMPM CHAR(2)
DECLARE @FILENAME CHAR(100)
DECLARE @FILEGEN VARCHAR(255)
DECLARE @SUBJECTDATE VARCHAR(100)
DECLARE @AUTHID VARCHAR(15)
DECLARE @RESPONSECODE VARCHAR(10)
SELECT @MONTH = SUBSTRING(CONVERT(CHAR(10), DATEADD(DAY, 0, GETDATE()),101),1,2),
@DAY = SUBSTRING(CONVERT(CHAR(10),DATEADD(DAY, 0, GETDATE()),101),4,2),
@YEAR = SUBSTRING(CONVERT(CHAR(10),DATEADD(DAY, 0, GETDATE()),101),7,4)
SELECT @HOUR = SUBSTRING(CONVERT(VARCHAR, GETDATE(), 109),13,2),
@MINUTES = SUBSTRING(CONVERT(VARCHAR, GETDATE(), 109),16,2),
@SECONDS = SUBSTRING(CONVERT(VARCHAR, GETDATE(), 109),19,2),
@AMPM= SUBSTRING(CONVERT(VARCHAR, GETDATE(), 109),25,2),
@DATENOW = CONVERT(VARCHAR,GETDATE(),120)
set @AUTHID = '000000'
set @RESPONSECODE = '00'
SELECT @FILENAME
DECLARE @CSRI VARCHAR(4000)
DECLARE @QUERY VARCHAR(8000)
DECLARE @DELPREVFILE VARCHAR(4000)
--CREATE FILE ATTACHMENT
SET @FILENAME = 'Claims Tagged in MMS ' + @YEAR + @MONTH + @DAY + @HOUR + @MINUTES + @SECONDS + @AMPM + '.xls'
SET @FILEGEN = 'C:\'+@FILENAME
SET @QUERY = 'BCP "EXEC ClaimsProcessDoctorLab.dbo.newProc"'
SET @DELPREVFILE = 'del "C:\'+@FILENAME+'"'
SET @CSRI = @QUERY + ' queryout " C:\'+@FILENAME+'" -c -q -C1252 -Uwebuser -Pw3bu53r -S210.8.0.239\MNISQLDB -t "|"'
DECLARE @TOTAL DECIMAL(10,2)
DECLARE @COUNT INTEGER
--GET CLAIM COUNT
SET @COUNT = (SELECT COUNT(A.CLAIMNO) FROM (SELECT c.ClaimNo, c.CardNo, c.PayorCode, c.ProviderCode, p.ProviderName, c.PatientName, c.TotalDue, c.AdmissionDate, b.DateSubmitted, c.TrxnDate, c.AuthorizationCode as SystemTrace, (CONVERT(VARCHAR, GETDATE(), 112) + CONVERT(VARCHAR, c.ClaimNo)) AS RetrievalNo, @AUTHID as AuthResponseID, @RESPONSECODE AS ResponseCode, (DATEDIFF(hour, c.AdmissionDate, @DATENOW)) AS ClaimHour from Claims c
INNER JOIN ProviderInfo..[Providers] p on c.ProviderCode = p.ProviderCode
INNER JOIN BatchHeader b on c.BatchNo = b.BatchNo and c.ProviderCode = b.ProviderCode
where c.ClaimStatus = '99' and b.Status = 'CLOSED' and b.BatchStatus = 'PAID') A where A.ClaimHour >=8);
IF @TOTAL IS NULL
BEGIN
SET @TOTAL = 0
END
IF (@COUNT > 0)
BEGIN
--check for previously generated textfile (delete if exists)
EXEC master..xp_cmdshell @DELPREVFILE
--extract date and populate text file
EXEC master..xp_cmdshell @CSRI
END
--SEND EMAIL JOB
DECLARE @EMAILBODY VARCHAR(6000)
--IF THERE ARE CLAIMS TO EXTRACT
SET @SUBJECTDATE = 'ClaimsTaggedInMMS ' + ' - ' + @MONTH + '/' + @DAY + '/' + @YEAR + ' ' + @HOUR + ':' + @MINUTES + ':' + @SECONDS + ' ' + @AMPM
IF (@COUNT > 0)
BEGIN
SET @EMAILBODY = '<font face="Calibri" size="10" color="#003366">'+ 'Dear Sir/ Ma’am, '+ '<br/><br/>'+ 'These are the generated claims that are tagged in MMS.'+ '<br/><br/>'
SET @EMAILBODY = @EMAILBODY +' '+'Total Count : '+'<b>'+CONVERT(VARCHAR(10),@COUNT)+'</b>'+'<br/><br/>'
SET @emailbody = @emailbody +' '+'Best Regards, '+'<br/>'
SET @emailbody = @emailbody +' '+'System Admin '+'</font><br/><br/>'
SET @emailbody = @emailbody +' '+'<font face="Calibri" size="1" color="#003366">'+ 'Note: This mail is system generated. Do not reply to this email.'+'</font>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'ClaimsTaggedInMMS' --'PL_mailprofile'
, @recipients = N'[email protected];'
, @blind_copy_recipients = N'[email protected]'
, @file_attachments= @FILEGEN
, @subject = @SUBJECTDATE
, @body = @emailbody
, @body_format = N'HTML'
END
--NO CLAIMS TO EXTRACT
IF (@COUNT = 0)
BEGIN
SET @emailbody = '<font face="Calibri" size="10" color="#003366">'+ 'Dear Sir/ Ma’am, '+ '<br/><br/>'+ 'No claims were tagged in MMS.'+ '<br/><br/>'
SET @emailbody = @emailbody +' '+'Total Count : '+'<b>0</b>'+'<br/><br/>'
SET @emailbody = @emailbody +' '+'Best Regards, '+'<br/>'
SET @emailbody = @emailbody +' '+'System Admin '+'</font><br/><br/>'
SET @emailbody = @emailbody +' '+'<font face="Calibri" size="1" color="#003366">'+ 'Note: This mail is system generated. Do not reply to this email.'+'</font>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'ClaimsTaggedInMMS' --'PL_mailprofile'
, @recipients = N'[email protected];'
, @blind_copy_recipients = N'[email protected]'
, @subject = @SUBJECTDATE
, @body = @emailbody
, @body_format= N'HTML'
END
你能帮助我,为了成功完成这项工作,我应该采取什么样的解决方案,因为我是新手做这种事情的?谢谢!
我有同样的问题,这是一个权限问题。当exec sp_send_dbmail时,它将使用数据库引擎帐户来访问文件共享。您必须使用域帐户进行数据库引擎服务,并为该目录授予帐户权限。
这真的是许可问题。我这样测试我的脚本:
USE msdb
EXEC sp_send_dbmail
@profile_name = 'profilename',
@recipients = '[email protected]',
@subject='Test Attachment with Two Files',
@body='Two files have been included in this email.',
@file_attachments='C:\inetpub\test\Log\Log.txt;C:\Users\myname\Downloads\test.txt'
它适用于第一个文件,我向用户(本地机器\用户)添加了权限。这是因为我使用Windows身份验证登录到sql server。在我向用户(本地机器\用户)授予第二个文件的权限后,我可以将这两个文件作为附件发送。一旦成功,就可以将其添加到sql server代理作业中。下载文件夹仅用于测试。
Database Mail使用当前用户的Microsoft Windows安全上下文来控制对文件的访问。因此,使用SQL Server身份验证进行身份验证的用户无法使用@fileattachments附加文件。 Windows不允许SQL Server从远程计算机向另一台远程计算机提供凭据。因此,如果从运行SQL Server的计算机以外的计算机运行命令,则数据库邮件可能无法从网络共享附加文件。
但上面也为我工作了...当exec sp_send_dbmail时,它将使用数据库引擎帐户来访问文件共享。您必须使用域帐户进行数据库引擎服务,并为该目录授予帐户权限。
它将接受任何格式,唯一需要注意的是,您需要将serverName \ Users添加到文件所在的安全性下的用户列表中。然后邮件将被发送而没有任何问题。
谢谢