如何使用TSQL遍历文件夹中的所有文件? [英] How can I loop through all the files in a folder using TSQL?

查看:190
本文介绍了如何使用TSQL遍历文件夹中的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个excel文件的文件夹,我们想要使用TSQL导入我们的数据库。我们有使用 OpenRowSet 导入单个文件的代码,但是需要找到一种方法来遍历文件夹中的文件并在每个文件上运行此代码。如何使用TSQL实现这一目标?

We have a folder of excel files that we want to import into our database using TSQL. We have the code to import the individual file using OpenRowSet, but need to find a way to loop through the files in the folder and run this code on each file. How can this be accomplished using TSQL?

推荐答案

做了一些研究,并找到了一种方法来循环使用这样的文件:

Did some research, and found a way to loop over the files using something like this:

CREATE TABLE #tmp(excelFileName VARCHAR(100));
INSERT INTO #tmp
EXEC xp_cmdshell 'dir /B c:\my\folder\path\';

declare @fileName varchar(100)

While (Select Count(*) From #tmp where excelFileName is not null) > 0
Begin

    Select Top 1 @fileName = excelFileName From #tmp

    -- OPENROWSET processing goes here, using @fileName to identify which file to use

    Delete from #tmp Where excelFileName = @FileName

End

DROP TABLE #tmp

这篇关于如何使用TSQL遍历文件夹中的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆