I'm developing and testing an SSIS package in SQL 2012 to import the rows from a text file into a staging table(that is truncated before each execution) using a Script Task. Then an Execute SQL task is used to run a stored procedure, which then queries the staging table and formats the data for insertion into a final table.
Presently I am only testing with two different files. These files are separate emails received daily, which I have configured in Outlook to save as text files. Each file is renamed like YYYYDDMM_HHMMSS_[filename].txt. My SSIS package uses a For Each loop to assign this filename to a variable, @AckFileName, which I pass into my stored procedure and parse the file datetime from it.
So the first line of my stored procedure is:
SELECT DISTINCT CONVERT(datetime,left(@AckFileName,8)+' '+substring(@AckFileName,10,2)+':'+substring(@AckFileName,12,2)+':'+substring(@AckFileName,14,2))
When I run the stored procedure in SSMS it works correctly:
Exec [dbo].[ParseAckFile] @AckFileName
However, even though I have the same identical code for my SQL Statement in my Execute SQL task, I receive the following error:
"Exec [dbo].[ParseAckFile]..." failed with the following error: "Conversion failed when converting date and/or time from a character string."
Ok, so this seems simple/obvious enough...except the query in my stored procedure is converting the string to datetime, so it should also return an error when run manually, correct? I've tested this with each file, and the results are the same - the proc runs fine manually with either file, but the Execute SQL task fails.
Additionally: I've tried setting Delay Validation = True within the Execute SQL task, using either an OLEDB or ADO.NET connection to my final table, and using an expression within the Execute SQL task to build my query. None of these attempts made any difference to the error message I am receiving.
Any other suggestions I can try?