I've only written one stored procedure in the past and I'm being tasked again to write another one. The purpose of this SP is to do a bulk insert from a .txt file and then to move the file to an "archive" when it's done with the bulk insert. The archive part will be another SP but I'm having issues with the bulk insert part. Here's the code:
create procedure ImportFiles
@FilePath varchar(1000) = 'c:\Transfer\' ,
@ArchivePath varchar(1000) = 'c:\Transfer\Archive\' ,
@FileNameMask varchar(1000) = 'bcp*.txt' ,
@MergeProc varchar(128) = 'MergeBCPData'
AS
set nocount on
declare @ImportDate datetime
select @ImportDate = getdate()
declare @FileName varchar(1000) ,
@File varchar(1000)
declare @cmd varchar(2000)
create table ##Import (s varchar(8000))
create table #Dir (s varchar(8000))
/*****************************************************************/
-- Import file
/*****************************************************************/
select @cmd = 'dir /B ' + @FilePath + @FileNameMask
delete #Dir
insert #Dir exec master..xp_cmdshell @cmd
delete #Dir where s is null or s like '%not found%'
while exists (select * from #Dir)
begin
[B]select @FileName = min(s) from #Dir
select @File = @FilePath + @FileName
select @cmd = 'bulk insert'
select @cmd = @cmd + ' ##Import'
select @cmd = @cmd + ' from'
select @cmd = @cmd + ' ''' + replace(@File,'"','') + ''''
select @cmd = @cmd + ' with (FIELDTERMINATOR=''|'''
select @cmd = @cmd + ',ROWTERMINATOR = ''' + char(10) + ''')'[/B]
truncate table ##Import
-- import the data
exec (@cmd)
-- remove filename just imported
delete #Dir where s = @FileName
exec @MergeProc
The problem I'm having is with the select commands, I have no idea what to put in here. Can someone please help?
Thanks