Я пытаюсь прочитать файлы из пути и загрузить данные в таблицу, используя файл .fmt, но некоторые файлы не удается загрузить с ошибкой «Невозможно получить строку от поставщика OLE DB« BULK »для связанного сервера» (null) ""
Мой код sql есть, а мой файл fmt такой, как показано ниже
DECLARE @RowCnt int, @filename varchar(256), @filenumber int, @sql NVARCHAR(MAX)
--Create the table to store file list
CREATE TABLE #myFileList (FileNumber INT IDENTITY,FileName VARCHAR(256))
CREATE TABLE #FileStatus (FileNumber INT ,FileName VARCHAR(256), status varchar(255))
--Insert file list from directory to SQL Server
DECLARE @Path varchar(256) = 'C:\TEMP\2019\8Aug2019\'
DECLARE @Path1 varchar(256) = 'dir ' + @Path
DECLARE @Command varchar(1024) = @Path1 + ' /A-D /B'
INSERT INTO #myFileList
EXEC MASTER.dbo.xp_cmdshell @Command
DECLARE FileList CURSOR LOCAL FAST_FORWARD
FOR SELECT FileNumber, FileName from #myFileList -- WHERE FileNumber in (1,2,3,4,5)
--FOR select SvrName, SvrStatus from SQLDBMAIL.DBO.LSvrTable
OPEN FileList
FETCH NEXT FROM FileList INTO @filenumber, @filename
WHILE @@FETCH_STATUS=0
BEGIN
begin try
SET @sql = N' INSERT INTO OBITABLE
SELECT A.RECTYPE, A.RECNO, A.ACCTNO, A.AMOUNT,
A.DRCR, A.EFFDATE, A.REFERENCE, A.BOTRANTYPE,
A.FETRANTYPE, A.TRANPLACE, A.CARDNO,
A.ISOTERMID, A.ISOMERCHID, A.RETREFNO,
A.ACQBIN
from OPENROWSET(BULK ''' + @Path +@filename+ ''',
FORMATFILE = ''C:\TEMP\OBIBackup\OBIFMT.fmt'',
FIRSTROW = 1) AS A WHERE [RECTYPE]=''RCTP10'''
EXEC sp_executesql @sql
insert into #FileStatus
select @filenumber, @filename,'OK'
end try
begin catch
insert into #FileStatus
select @filenumber, @filename,ERROR_MESSAGE()+'ERROR'
end catch
FETCH NEXT FROM FileList INTO @filenumber, @filename
END
CLOSE FileList
DEALLOCATE FileList
select * from #FileStatus where status like '%ERROR'
DROP TABLE #myFileList
GO
DROP TABLE #FileStatus
GO
/ * FMT FILE * /
10.0
25
1 SQLCHAR 0 8 "" 1 RECTYPE SQL_Latin1_General_Cp437_BIN
2 SQLCHAR 0 12 "" 2 RECNO SQL_Latin1_General_Cp437_BIN
3 SQLCHAR 0 32 "" 3 ACCTNO SQL_Latin1_General_Cp437_BIN
4 SQLCHAR 0 9 "" 0 BlankFl SQL_Latin1_General_Cp437_BIN
5 SQLCHAR 0 12 "" 4 AMOUNT ""
6 SQLCHAR 0 2 "" 5 DRCR SQL_Latin1_General_Cp437_BIN
7 SQLCHAR 0 4 "" 0 BlankFl SQL_Latin1_General_Cp437_BIN
8 SQLCHAR 0 8 "" 6 EFFDATE SQL_Latin1_General_Cp437_BIN
9 SQLCHAR 0 1 "" 0 BlankFl SQL_Latin1_General_Cp437_BIN
10 SQLCHAR 0 16 "" 7 REFERENCE SQL_Latin1_General_Cp437_BIN
11 SQLCHAR 0 1 "" 0 BlankFl SQL_Latin1_General_Cp437_BIN
12 SQLCHAR 0 8 "" 8 BOTRANTYPE SQL_Latin1_General_Cp437_BIN
13 SQLCHAR 0 37 "" 0 BlankFl SQL_Latin1_General_Cp437_BIN
14 SQLCHAR 0 8 "" 9 FETRANTYPE SQL_Latin1_General_Cp437_BIN
15 SQLCHAR 0 1 "" 0 BlankFl SQL_Latin1_General_Cp437_BIN
16 SQLCHAR 0 40 "" 10 TRANPLACE SQL_Latin1_General_Cp437_BIN
17 SQLCHAR 0 32 "" 11 CARDNO SQL_Latin1_General_Cp437_BIN
18 SQLCHAR 0 236 "" 0 BlankFl SQL_Latin1_General_Cp437_BIN
19 SQLCHAR 0 8 "" 12 ISOTERMID SQL_Latin1_General_Cp437_BIN
20 SQLCHAR 0 15 "" 13 ISOMERCHID SQL_Latin1_General_Cp437_BIN
21 SQLCHAR 0 8 "" 0 BlankFl SQL_Latin1_General_Cp437_BIN
22 SQLCHAR 0 12 "" 14 RETREFNO SQL_Latin1_General_Cp437_BIN
23 SQLCHAR 0 16 "" 0 BlankFl SQL_Latin1_General_Cp437_BIN
24 SQLCHAR 0 6 "" 15 ACQBIN SQL_Latin1_General_Cp437_BIN
25 SQLCHAR 0 141 "\r\n" 0 BlankFl SQL_Latin1_General_Cp437_BIN