首页 - 数据库 - SQL Server

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

发布时间: 2007-04-19 05:04    作者: 未知    来源: 未知    浏览:    评论


/*--原帖地址: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

TAG

Smile Big Smile Surprise Stick out tongue Wink Sad Tongue Tied Indifferent Crying Embarrassed Cool Angry Angel Devil [8-|] [:#] [:-*] [:^)] [<:o)] [|-)] Yes Beer Left Hug Music Star Time Snail Pizza Automobile Umbrella Computer Storm [mo] [8o|] [^o)] [+o(] [*-)] [8-)] Coffee No Drinks [Z] Right Hug Cake Broken Heart Gift Wilted Flower Movie Dog Idea Sleep Email Travel Paradise
呢称:

加粗 斜体 下划线 链接 图片 代码 邮件地址 引用 列表

最多只能输入100个字符

Tags

SQL 数据库 asp.net C# XML 控件 .NET教程 程序 事件 数据 安全 代码 Server 客户端 验证 数据库专栏 接口 文件 Oracle DataSet 函数 DataGrid 问题 .net return C#语言 JavaScript 服务 IIS 对象 语句 windows 继承 时间 web.config 设计 开发 参数 变量 解决 字符 ADO.net 环境 VB.Net语言 web 异常 工具 服务器 计算 实例 OLEDB Application VB Word WebService insert asp net 安装 记录

精华推荐

更多

精品下载

更多