Attachment File is Invalid in SQL Server 2008 R2

igsr9ssn  于 2023-02-28  发布在  SQL Server
关注(0)|答案(3)|浏览(223)

Good day! I am currently creating a job that should generate an auto-email containing Claims Tagged Yesterday. I've done creating the job and creating the store procedure in getting the data which will be inside an Excel file. When I try to execute the job I created, it gives me an error like this:
Msg 22051, Level 16, State 1, Line 0
Attachment file C:\Claims Tagged in MMS 20141013111305AM.xls is invalid.

I wonder where did this mistake occur. I tried other possible solutions but still I get the same error. I also thought that I might not have an access in creating or insert files in the server.

Here is my JOB:

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'sample_email@mail.com;'
      , @blind_copy_recipients = N'sample_email@mail.com'
      , @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'sample_email@mail.com;'
      , @blind_copy_recipients = N'sample_email@mail.com'
      , @subject    = @SUBJECTDATE
      , @body       = @emailbody
      , @body_format= N'HTML'

END

Can you please help me what solution should I do to successfully run the job as I am new to doing this kind of things? Thank you!

j5fpnvbx

j5fpnvbx1#

I had the same problem, it was a permissions issue. When exec sp_send_dbmail, it will use the database engine account to access the file share. You'll have to use a domain account for the database engine service, and give the account permissions to the directory.

nbysray5

nbysray52#

Database Mail uses the Microsoft Windows security context of the current user to control access to files. Therefore, users who are authenticated with SQL Server Authentication cannot attach files using @fileattachments. Windows does not allow SQL Server to provide credentials from a remote computer to another remote computer. Therefore, Database Mail may not be able to attach files from a network share in cases where the command is run from a computer other than the computer that SQL Server runs on.

But the above worked for me also...When exec sp_send_dbmail, it will use the database engine account to access the file share. You'll have to use a domain account for the database engine service, and give the account permissions to the directory.

It will accept any format and the only thing to take note is that you need to add serverName\Users to users list under security where the file is there.Then the mail will be sent without any issues.

Thanks

ruarlubt

ruarlubt3#

It really is permission issue. I test my script this way:

USE msdb
EXEC sp_send_dbmail 
@profile_name = 'profilename',
@recipients = 'test@email.com',
@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'

It works for the first file, which I added permission to Users(local machine\Users). It's because I use windows authentication to login to sql server. After I gave permission to Users(local machine\Users) to the 2nd file, I can send both files as the attachments. Once it's successful this way, you can add this to your sql server agent job. Downloads folder is for testing only.

相关问题