加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

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

发布时间:2018-08-23 04:11:11 所属栏目:站长百科 来源:站长网
导读:/*--原帖地址: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','3

/*--原帖地址: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)  --导出的文本文件保存的目录asdeclare @s nvarchar(4000)if isnull(@path,'')='' set @path='c:'else if right(@path,1)<>'' set @path=@path+''

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

--调用exec p_export 'c:'go

--删除测试drop table tbdrop proc p_export

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读