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)
 select into text file tab delimited

Author  Topic 

pr4t3ekd
Starting Member

31 Posts

Posted - 2009-12-08 : 19:14:46
Hello,

I have the following select statement:

SELECT TOP 100 * FROM TABLE

Please provide me with code where i can extract this to location such as:

\\server\folder\subfolder\file.txt

File must include the headers.

I am using SQL Server 2000

Thank You

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-08 : 21:54:01
You can use DTS or the import/export wizard. If you use bcp.exe, it wouldn't include the headers without extra work.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

pr4t3ekd
Starting Member

31 Posts

Posted - 2009-12-09 : 00:08:37
hello, here is my solution:

--use this to run & actually save file

DECLARE @path VARCHAR(8000),
@bcpCommand varchar(2000)
SET @path = '\\SERVER\' + 'DATA_' + CONVERT(CHAR(8), GETDATE()-1, 112) + '.txt'
SET @bcpCommand = 'bcp "SELECT * FROM DATA" queryout "'
SET @bcpCommand = @bcpCommand + @path + '" -T -c -t \t'
EXEC master..xp_cmdshell @bcpCommand

--to get headers, use a view called DATA and insert headers as a row itself.
--e.g.
SELECT 'COLUMN1 COL1, 'COLUMN2' COL2
UNION ALL
SELECT 'DATA', 'DATA
FROM TABLE
Go to Top of Page
   

- Advertisement -