Export Text/ntext field value per record as a text file
Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
/*--Original post address: http://community.csdn.net/expert/topic/3851/3851741.xml?temp=.4726831--*/
--Test data CREATE TABLE TB (ID varchar (m) primary key,detail text) Insert TB Select ' AAA ', ' 11111 ' union ALL Select ' BBB ', ' 43424 ' u Nion all SELECT ' CCC ', ' 324234 '
The detail field in the above table is exported as a text file, requiring that each record be a file named Id+.txt the data in the above table is exported to Aaa.txt,bbb.txt,ccc.txt--*/go
--Processed stored procedures create proc P_export@path nvarchar (1000)--Exported text file saved directory asdeclare @s nvarchar (4000) if IsNull (@path, ') = ' SET @ Path= ' C:\ ' else if Right (@path, 1) <> ' Set @path = @path + ' \ '
--To build a BCP export statement for each record with a cursor, the syntax reference for BCP is SQL online help 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_sta Tus=0begin-Calls xp_cmdshell stored procedures to perform BCP for export processing exec master. xp_cmdshell @s,no_output fetch TB into @sendclose tbdeallocate tbgo
--Call exec p_export ' C:\ ' Go
--Delete test drop table Tbdrop proc P_export
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
and provide relevant evidence. A staff member will contact you within 5 working days.