逐記錄導出text/ntext字段值為文本文件
2024-07-21 02:05:36
供稿:網友
網站運營seo文章大全提供全面的站長運營經驗及seo技術!
/*--原帖地址: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 [email protected] nvarchar(1000) --導出的文本文件保存的目錄asdeclare @s nvarchar(4000)if isnull(@path,'')='' set @path='c:/'else if right(@path,1)<>'/' set @[email protected]+'/'
--用游標構建每條記錄的bcp導出語句,bcp的語法參考sql聯機幫助declare tb cursor localforselect 'bcp "select detail from ' +quotename(db_name()) +'..tb where id=' +quotename(id,n'''') +'" queryout "'[email protected] +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