Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Create a text file using SQL commands

Author  Topic 

yazasi
Starting Member

11 Posts

Posted - 2010-08-06 : 18:18:15
Hi all,

I do have a local table within SQL server from which I need to create a text file to my C:\ drive and name it EXAMPLE.TXT

Select *
from TABLE(A)

All returned records need to go to a text file that should be created within SQL commands.

I don't want to use the right click then save result as .txt.

Again this needs to be executed within a long script that should generate a final local table from which a text file should be created on my C:\ drive.

Thank you for your help.

KlausEngel
Yak Posting Veteran

85 Posts

Posted - 2010-08-06 : 18:32:27
Try using the BCP command functionality. You may first have to enable the use of xp_cmdshell:
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE

Then use something like this.
EXEC xp_cmdshell 'bcp "SELECT * FROM YourTable" queryout "C:\testfile.txt" -T -c -t,'

this should work for the problem described.
Go to Top of Page

yazasi
Starting Member

11 Posts

Posted - 2010-08-09 : 13:39:44
Thanks it did work just fine.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-09 : 14:12:31
well, it certainly did NOT save it to your C:\ drive...it saved it to the servers C:\ drive

Also, you didn't specify the text file format you need it in..I'm guessing tab delimited is ok



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -