数据库

本类阅读TOP10

·SQL语句导入导出大全
·Power Designer杂记
·SQL Server日期计算
·常用的oracle函数使用说明(一)
·sqlserver2000数据库置疑的解决方法
·MS SQLServer OLEDB分布式事务无法启动的一般解决方案
·SQL to Excel 的应用
·SQL语句导入导出大全
·Error:ORA-01033:ORACLE initialization or shutdown in progress错误解决
·Oracle中password file的作用及说明

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
逐记录导出text/ntext字段值为文本文件

作者:未知 来源:月光软件站 加入时间:2005-5-13 月光软件站

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




相关文章

相关软件




月光软件程序下载编程文档电脑教程网站设计网址导航网络文学游戏天地幽默笑话生活休闲写作范文安妮宝贝
电脑技术编程开发网络专区谈天说地情感世界游戏元素分类游戏热门游戏体育运动手机专区业余爱好影视沙龙
音乐天地数码广场教育园地科学大观古今纵横谈股论金人文艺术医学保健动漫图酷二手专区地方风情各行各业

月光软件站·版权所有