SQLUSA

Microsoft SQL Server 2008 Best Practices

How to import filenames in a folder into a table?

 

Execute the following script in Query Editor create a stored procedure which reads a file folder to get the filenames:

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

 

declare @path sysname

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

exec procReadFolder @path

go

 

 

SQLUSA - The Best SQL Server 2008 Training in the World
 
 
SQLUSA.com Home Page