简体   繁体   English

将多个文本文件导入SQL Server

[英]Import multiple text files into SQL Server

I am using SQL Server 2017 Express edition on Microsoft Windows 10. I want to import all text files in a directory into a database at one time. 我正在Microsoft Windows 10上使用SQL Server 2017 Express版。我想一次将目录中的所有文本文件导入数据库。 My idea is that that SQL Server loops over the files in that directory and imports them all at the same time. 我的想法是,SQL Server遍历该目录中的文件并同时将它们全部导入。 Is there a way that I can achieve this? 有没有办法可以做到这一点?

Best Regards, 最好的祝福,

declare @dir varchar(1000) = 'C:\Temp';
declare @command varchar(1000);
declare @files table (id int identity, filename varchar(1000));

set @command = 'dir /b ' + @dir;
insert into @files execute xp_cmdshell @command;

declare commands cursor for 
    select 'bulk insert <your table name here> from ''' + @dir + '\' + filename + ''' <other options, WITH FORMAT, etc. here>;' from @files;
open commands;
fetch commands into @command;
while (@@fetch_status = 0) begin
    --print @command;
    execute(@command);
    fetch commands into @command;
end;
close commands;
deallocate commands;

Modify @dir and the bulk insert command that is being build and you're done. 修改@dir和正在构建的大容量插入命令,您已完成。

You may have to enable 'xp_cmdshell', and this could be a problem for your DBA; 您可能必须启用“ xp_cmdshell”,这对于您的DBA可能是个问题; and using 'execute' is always a potential issue (SQL injection, etc.). 并且使用“执行”始终是一个潜在的问题(SQL注入等)。

To enable xp_cmdshell: 要启用xp_cmdshell:

-- To allow advanced options to be changed.  
EXEC sp_configure 'show advanced options', 1;  
GO  
-- To update the currently configured value for advanced options.  
RECONFIGURE;  
GO  
-- To enable the feature.  
EXEC sp_configure 'xp_cmdshell', 1;  
GO  
-- To update the currently configured value for this feature.  
RECONFIGURE;  
GO

As noted in another answer, xp_commandshell is problematic. 如另一个答案所述, xp_commandshell是有问题的。 SQL Server 2016+ allows another approach. SQL Server 2016+允许另一种方法。 See example 看例子

declare @tbl table(fn varchar(255), depth int,[isfile] int,primary key(fn))
declare @dir varchar(50)='C:\temp\' --'starting dir
insert @tbl
EXEC Master.dbo.xp_DirTree @dir,1,1 --dir, depth (1 - current only), file (0 - dirs only, 1+ - dirs and files)

delete @tbl where [isfile]=0 or fn not like '%.txt' --keep .txt files only
--select * from @tbl
--will insert into this table
create table #fileTbl (id int identity(1,1) primary key,fn varchar(255),txt varchar(max))
declare @fn varchar(255), @query nvarchar(4000)
select top 1 @fn=fn from @tbl
while @@ROWCOUNT>0--number of rows from the last query executed
begin
--dynamic query required to build OPENROWSET
set @query='insert #fileTbl(fn,txt) select '''+@dir+@fn+''',BulkColumn from openrowset(bulk ''c:\temp\'+@fn+''',single_blob) t'
exec sp_executesql @query
delete @tbl where fn=@fn
select top 1 @fn=fn from @tbl --proceed
end
select * from #fileTbl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM