title: Attachment File is Invalid in SQL Server 2008 R2
body: <p>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 <strong>Excel file</strong>. When I try to execute the job I created, it gives me an error like this:</p>
  
  <blockquote>
    <p>Msg 22051, Level 16, State 1, Line 0<br>
    Attachment file C:\Claims Tagged in MMS 20141013111305AM.xls is invalid.</p>
  </blockquote>
  
  <p>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. </p>
  
  <p>Here is my JOB:</p>
  
  <pre><code>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 &gt;=8);
  
  IF @TOTAL IS NULL 
  BEGIN
      SET @TOTAL = 0
  END
  
  IF (@COUNT &gt; 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 &gt; 0)
  
  BEGIN
  
      SET @EMAILBODY = '&lt;font face="Calibri" size="10" color="#003366"&gt;'+ 'Dear Sir/ Ma’am, '+ '&lt;br/&gt;&lt;br/&gt;'+ 'These are the generated claims that are tagged in MMS.'+ '&lt;br/&gt;&lt;br/&gt;'   
      SET @EMAILBODY = @EMAILBODY +' '+'Total Count :  '+'&lt;b&gt;'+CONVERT(VARCHAR(10),@COUNT)+'&lt;/b&gt;'+'&lt;br/&gt;&lt;br/&gt;' 
      SET @emailbody = @emailbody +' '+'Best Regards, '+'&lt;br/&gt;'
      SET @emailbody = @emailbody +' '+'System Admin  '+'&lt;/font&gt;&lt;br/&gt;&lt;br/&gt;'
      SET @emailbody = @emailbody +' '+'&lt;font face="Calibri" size="1" color="#003366"&gt;'+ 'Note: This mail is system generated. Do not reply to this email.'+'&lt;/font&gt;'
      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 = '&lt;font face="Calibri" size="10" color="#003366"&gt;'+ 'Dear Sir/ Ma’am, '+ '&lt;br/&gt;&lt;br/&gt;'+ 'No claims were tagged in MMS.'+ '&lt;br/&gt;&lt;br/&gt;'   
      SET @emailbody = @emailbody +' '+'Total Count :  '+'&lt;b&gt;0&lt;/b&gt;'+'&lt;br/&gt;&lt;br/&gt;' 
      SET @emailbody = @emailbody +' '+'Best Regards, '+'&lt;br/&gt;'
      SET @emailbody = @emailbody +' '+'System Admin  '+'&lt;/font&gt;&lt;br/&gt;&lt;br/&gt;'
      SET @emailbody = @emailbody +' '+'&lt;font face="Calibri" size="1" color="#003366"&gt;'+ 'Note: This mail is system generated. Do not reply to this email.'+'&lt;/font&gt;'
      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
  </code></pre>
  
  <p>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!</p>