逐记录导出text/ntext字段值为文本文件

/*--原帖地址:
http://community.csdn.net/Expert/topic/3851/3851741.xml?temp=.4726831
--*/

--测试数据
create table tb(id varchar(50) primary key,detail text)
insert tb select 'aaa','11111'
union all select 'bbb','43424'
union all select 'ccc','324234'

/*--处理要求

把上述表中的detail字段导出为文本文件,要求每条记录一个文件,文件名为id+.txt
即上述表中的数据要求导出为 aaa.txt,bbb.txt,ccc.txt
--*/

go

--处理的存储过程
create proc p_export
@path nvarchar(1000) --导出的文本文件保存的目录
as
declare @s nvarchar(4000)
if isnull(@path,'')='' set @path='c:/'
else if right(@path,1)<>'/' set @path=@path+'/'

--用游标构建每条记录的bcp导出语句,BCP的语法参考sql联机帮助
declare tb cursor local
for
select 'BCP "select detail from '
+quotename(db_name())
+'..tb where id='
+quotename(id,N'''')
+'" queryout "'+@path
+id+'.txt" /T /w'
from tb
open tb
fetch tb into @s
while @@fetch_status=0
begin
--调用xp_cmdshell存储过程执行bcp进行导出处理
exec master..xp_cmdshell @s,no_output
fetch tb into @s
end
close tb
deallocate tb
go

--调用
exec p_export 'c:/'
go

--删除测试
drop table tb
drop proc p_export

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值