FREE TRIAL  SQL 2012 PROGRAMMING  SEARCH
SQL Server Scripts SQL 2005 SQL 2008 Articles
SQL JOBS NEWS FORMAT DEV JOBS
How to import filenames in a folder into a table?

Execute the following Microsoft SQL Server 2008 T-SQL script in SSMS Query Editor create a stored procedure which reads a file folder to get the filenames.

Warning article:

What are the security risks using cmdexec?

-- Create stored procedure with one input parameter

 

use AdventureWorks

go

create procedure 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

end

go

 

-- Execute stored procedure

declare @path sysname

set @path = 'F:\data\export\'

exec procReadFolder @path

go

Related article:

Import multiple Files to SQL Server using T-SQL 

 

Exam Prep 70-461
Exam 70-461