在某个文件夹下有很多文件,要将该文件夹下的
snd*.txt 文件,插入 snd_info 表,文本文件处理对应的格式文件为 snd_bcp.txt
rcv*.txt 文件,插入 rcv_info 表,文本文件处理对应的格式文件为 rcv_bcp.txt
其他 .txt 文件和其他类型的文件不处理
---------------------------------------------------------------
--示例处理过程
--处理参数
declare @path nvarchar(266)
set @path='c:' --要导入的文件所在的目录
--导入处理
--得到该目录下的所有文件
if right(@path,1)<>'' set @path=@path+''
create table #t(fn nvarchar(1000),depth int,isfile int)
insert #t exec master..xp_dirtree @path=@path,@depth=1,@file=1
--定义游标,逐个导入文件
declare @s varchar(8000)
declare tb cursor local for
select 'bulk insert '
+case
when fn like 'snd%.txt'
then 'snd_info from '''+@path+fn
+''' with (formatfile='''+@path+'snd_bcp.txt'
else 'rcv_info from '''+@path+fn
+''' with (formatfile='''+@path+'rcv_bcp.txt'
end+''')'
from #t
where isfile=1
and fn not in('snd_bcp.txt','rcv_bcp.txt')
and (fn like 'snd%.txt' or fn like 'rcv%.txt')
open tb
fetch tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch tb into @s
end
close tb
deallocate tb
drop table #t