Home > database > How one-time import multiple SQL TXT to SQL but error, how to change?
How one-time import multiple SQL TXT to SQL but error, how to change?
Time:05-09
Import a TXT without problems,
If object_id (' tempdb for. Dbo. # test1 ') is not null drop table # test1 Go The create table # test1 ([notext] nvarchar (20))
BULK INSERT # test1 FROM 'E: \ LOTTData \ hellome \ 24 TXT' WITH ( FIELDTERMINATOR=", ROWTERMINATOR='\ n' )
GO SELECT * FROM # test1
But In the folder hellome have 1, 2, 3, 4,,,,,,, 333. TXT text documents, a total of several hundred TXT text documents, TXT text document file names for the digital 1. TXT 2. TXT 3. TXT . . 1223. TXT
Declare @ SQL nvarchar (20), @ I int Select SQL="@, @ I=1223 While @ i> 0 begin SQL="insert INTO the set @ # test1 Select the FROM OPENDATASOURCE (. ' 'MICROSOFT JET. The OLEDB. 4.0 "', 'Text; The DATABASE=E: \ LOTTData \ hellome \ ')... [' + convert (varchar, @ I) + '# TXT]' The exec (@ SQL) @ I=@ I - 1 End
SQL all TXT in the NOT NULL record (NOT empty data, each row should be no comma separator) one-time import multiple TXT to list, but run times wrong, how to change? TXT text documents in the empty data format is 1, 2, 3, 4, 5 in a row at the same time requirement: field notext format of 01 02, 03 04 05
CodePudding user response:
If object_id (' tempdb for. Dbo. # test1 ') is not null drop table # test1 Go The create table # test1 ([notext] nvarchar (20)) DECLARE @ SQL NVARCHAR (MAX), @ INT I; SELECT @ SQL=N '@ I=1223; WHILE @ I & gt; 0 The BEGIN SET @ SQL=N 'BULK INSERT # test1 FROM' E: \ LOTTData \ hellome \ '+ CONVERT (VARCHAR, @ I) + N ". TXT "' WITH ( FIELDTERMINATOR=' ' ' ', ROWTERMINATOR='\ n' ) '; PRINT @ SQL; The EXEC (@ SQL); The SET @ I=@ I - 1; END;