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 2000 Forums
 SQL Server Development (2000)
 Export Results to comma txt file SP?

Author  Topic 

headstrong
Starting Member

3 Posts

Posted - 2006-01-12 : 14:26:07
I was wondering if it's possible to create a stored Procedure, vbscript or in ASP to take the results of a query and export it to a text file on the SERVER, Fields need to be comma separated and all text fields need to be enclosed in "". also the filename.txt needs to be a variable so I can name it per query.

SELECT name,uid,id,address FROM mytable,locationid WHERE id=12


"bob smith",234,12,"123 street ave, NC",222
thanks for any help you can give me

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-01-12 : 15:00:09
You can do this with DTS or bcp. DTS is available in Enterprise Manager. bcp is a command line executable. I prefer bcp.

Tara Kizer
aka tduggan
Go to Top of Page

avmreddy17
Posting Yak Master

180 Posts

Posted - 2006-01-12 : 16:31:10
You can use do some thing like this...

bcp pubs.dbo.authors out c:\temp\pubauthors.bcp -n -S CCSQL -U sa -P temp1234

Thx
Venu
Go to Top of Page

headstrong
Starting Member

3 Posts

Posted - 2006-01-12 : 16:49:35
thanks for the repplay

I tried the bcp and I got the CSV file but how would I add the "" to selective fields?

for example around fields 1,4 below only and not the others.

"bob smith",234,12,"123 street ave, NC",222
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-12 : 22:09:39
you can do it in your query
SELECT '"'+ name + '"', uid, id, '"' + address + '"' FROM mytable,locationid WHERE id=12


-----------------
'KH'

if you can't beat them, have someone else to beat them
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-13 : 01:56:11
That requires you to convert the columns to varchar if they are integer data types

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-01-13 : 12:20:32
Yes, but headstrong only needs to do it with text columns. Headstrong, you'll need to use the queryout option for bcp.exe to use khtan's solution.

Tara Kizer
aka tduggan
Go to Top of Page
   

- Advertisement -