同一目录下批量文件导入数据库的存储过程

------------------------------------------------------------------------

--       Author : navy887(草根)

--       用途:同一目录下批量文件导入数据库

--       使用方法:EXEC SP_BupInTxt 'sa','123'

--       日期:2009.10.29

--       转载请注明出处

------------------------------------------------------------------------

CREATE PROCEDURE SP_BupInTxt
(
@user varchar(100) ='sa',  --数据库用户名
@password varchar(100)  --数据库密码
)
AS
BEGIN
create table bcpintxt(col varchar(200))
create table datatable(col varchar(200)) --创建存放数据的表,根据自己的字段数据可以再添加

declare @SQL varchar(8000),@file varchar(8000)
insert into bcpintxt(col) exec master..xp_cmdshell 'dir c:/test/*.txt /b'  --这里修改存放文件的目录
delete from bcpintxt where col is null
declare fetch_id cursor for select col from bcpintxt order by col
open fetch_id
fetch fetch_id into @file
while @@fetch_status=0
begin
 Set @SQL='Bcp db_test.dbo.datatable in c:/test/'+@file+' -S 127.0.0.1 -U '+@user+' -P '+@password+' -t /t -c'  --这里修改库名和路径

 --Set @SQL='Bcp db_test.dbo.datatable in c:/test/'+@file+' -t /t -c -T' --加上-T可以不需要用户名密码
 --print @sql
 EXEC master..xp_cmdshell @sql
 fetch next from fetch_id into @file
end
close fetch_id
deallocate fetch_id

drop table bcpintxt

END

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值