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
 General SQL Server Forums
 New to SQL Server Programming
 Export query to a flat file

Author  Topic 

robert693
Starting Member

42 Posts

Posted - 2009-08-18 : 14:57:38
I have a query that pulls data drom several tables. How can I dump the data to a flat file? Is the bcp Utility what I would use?

Thank you for your help!

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-08-18 : 16:43:34
exactly. here's a sample

bcp "YOUR SELECT QUERY HERE" queryout f:\datafile.txt -c -SserverNameHere -T


the stuff in red you need to change
Go to Top of Page

robert693
Starting Member

42 Posts

Posted - 2009-08-20 : 11:32:23
Do I run this from the command prompt?
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-08-20 : 12:54:41
yes
Go to Top of Page

shebert
Yak Posting Veteran

85 Posts

Posted - 2009-08-20 : 13:01:28
Couldn't you just set query results to file in query analyser?
Go to Top of Page

robert693
Starting Member

42 Posts

Posted - 2009-08-21 : 14:14:43
How can I do it in the query analyzer? I am in SQL Server 2005, I think that the analyzer is the query building area in this version.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-08-21 : 22:47:37
you could so it with SSMS, but don't. use BCP. SSMS would add headers, footer and not be formatted properly for importing later
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-08-22 : 08:58:35
u can use the XP_CmdShell, execute following in SSMS
XP_CmdShell 'bcp "YOUR SELECT QUERY HERE" queryout f:\datafile.txt -c -SserverNameHere -T'

Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page
   

- Advertisement -