I have this code in an SP that sets up a bulk insert:
begin try
declare @sentFile nvarchar(255)
declare @bulk_cmd nvarchar(1000) = ''
declare @loadDate nvarchar(8) = Convert(nvarchar(8),@p_loadDate) -- @p_loadDate is char(8)
set @StrImportFolder = N'D:\EMVImports\'
set @sentFile = @StrImportFolder + N'etl_rnli_sent_'+ @loadDate + N'.txt'
SET @bulk_cmd = N'BULK INSERT loadSent
FROM ''' + @sentFile + N'''
WITH (
FIRSTROW = 2
,formatfile=''D:\EMVScripts\Sent_Format.xml''
)'
Print @bulk_cmd
EXECUTE sp_executesql @bulk_cmd
-- more stuff happens here
end try
Inside my stored procedure, this fails with this error:
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
But the code printed out:
BULK INSERT loadSent
FROM 'D:\EMVImports\etl_sent_20130529.txt'
WITH (
FIRSTROW = 2
,formatfile='D:\EMVScripts\Sent_Format.xml'
)
works like a charm. I've no idea why it fails under sp_executesql
.