批量插入文件夹中的多个文件

时间:2015-10-28 13:44:40

标签: sql sql-server tsql

对于那些一次又一次帮助我的Stack的优秀人员......我正在尝试为文件夹中的所有文件运行批量插入例程。这是我的方法,但我似乎遇到了一个障碍,上面写着“我的选择声明中的访问被拒绝”

EXEC [dbo].[procReadFolder] 'C:\Users\ABC\Downloads\NYSE_2015'

我对所有文件夹和文件都有管理员权限,因此不确定下一步。

参见下面的逻辑:

ALTER procedure [dbo].[procReadFolder] @path sysname
as
begin
set nocount on

declare @dirContent table(
id int identity(1,1),
FileName sysname NULL
)
declare  @cmd nvarchar(512)
set @cmd = 'DIR /b ' + @path

insert into @dirContent
exec master..xp_cmdshell @cmd

select * from @dirContent

-- Code to Loop through all the records in the file
-- To be written

-- Routine that takes the file name as a parameter and inserts the file
EXEC [dbo].[BulkInsert] 'Path'

end

结果集:

1   Access is denied.
2   NULL

2 个答案:

答案 0 :(得分:2)

您需要确保运行SQL Server服务的帐户可以访问特定路径。

存储过程在配置为运行SQL Server服务的帐户的安全上下文下执行,因此需要为该驱动器上的文件夹授予权限。

答案 1 :(得分:0)

我将SQL服务帐户更改为另一个用户帐户“SQLService”而不是默认的NT / MSSQLServer帐户,并且它可以正常工作