x

How do I output the results of a simple SQL statement to a text file??

I have this simple SELECT statement but am struggling to output it to a file using SQL Server 2005:

                    
--QUOTER QUOTE DETAILS                    
                    
SELECT dbo.quoteproduct.quoteid, dbo.quoteproduct.productgroupid,                     
    dbo.product.productname, dbo.product.description, dbo.quoteproduct.price                    
FROM dbo.quoteproduct, dbo.product                    
WHERE dbo.quoteproduct.productid = dbo.product.productid;                    

In other versions of SQL like Sybase I can use the command OUTPUT TO as below but this doesnt work:

                    
OUTPUT TO 'D:\QuoterQuoteDetails.txt'                    
FORMAT ascii                     
DELIMITED BY '|'                    
QUOTE '';                    

After bing'ing it I have found that you can call an xp_cmdshell to output query results but this only seems to store data in a temporary table not a file.

Id appreciate any help on this.
Many thanks
Andy

more ▼

asked Nov 16, 2009 at 12:34 PM in Default

Andy Watts gravatar image

Andy Watts
2 2 2 2

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

You want BCP, something like:

DECLARE @cmd VARCHAR(8000)             
            
SET @cmd = 'bcp "...Select <fieldlist> from <table>..." queryout'             
    +' "D:\QuoterQuoteDetails.txt" -c -T'             
    +' -SLOCALHOST"'             
            
EXEC master..xp_cmdshell @cmd, NO_OUTPUT            

Should do the trick.

more ▼

answered Nov 16, 2009 at 12:40 PM

RickD gravatar image

RickD
1.7k 1 1 4

Hi RickD, thanks for your help and quick reply. If I understand your advice I produced the following which executes sucessfully however it still doesnt produce a file output any ideas?

DECLARE @cmd VARCHAR(8000) SET @cmd = 'bcp "...SELECT dbo.quoteproduct.quoteid,
dbo.quoteproduct.productgroupid, dbo.product.productname, dbo.product.description,
dbo.quoteproduct.price
FROM dbo.quoteproduct, dbo.product
WHERE dbo.quoteproduct.productid = dbo.product.productid..."queryout'
+' "D:\QuoterQuoteDetails.txt" -c -T'
+' -SLOCALHOST"'
EXEC master..xp_cmdshell @cmd, NO_OUTPUT
Nov 17, 2009 at 05:47 AM Andy Watts

This is the D: drive on your server, not your local machine. Also, you don't wan t the elipses [...]

DECLARE @cmd VARCHAR(8000) SET @cmd = 'bcp "SELECT dbo.quoteproduct.quoteid, dbo.quoteproduct.productgroupid, dbo.product.productname, dbo.product.description, dbo.quoteproduct.price FROM dbo.quoteproduct, dbo.product WHERE dbo.quoteproduct.productid = dbo.product.productid"queryout "D:\QuoterQuoteDetails.txt" -c -T -SLOCALHOST"'
EXEC master..xp_cmdshell @cmd, NO_OUTPUT
Nov 17, 2009 at 09:26 AM RickD
Using xp_cmdshell usually isn't the best way to go. Most larger companies don't allow this stored procedure to be enabled on SQL Servers as it is a security risk. However the BCP command can be run from a command line without issue to export the data.
Nov 30, 2009 at 04:18 AM mrdenny
(comments are locked)
10|1200 characters needed characters left

You can also right-click the query window: "Query Options | results | text" formatting options, then (right-click) query window and change "results to" to file.

This is a quick, one-time way redirect the output to a file.

more ▼

answered Nov 16, 2009 at 01:18 PM

TG gravatar image

TG
1.8k 1 3

Also thanks for your help - I went into the query options but couldnt find the option to output to file although I have to admit I would prefer to have it scripted so that it works automatically.
Nov 17, 2009 at 05:54 AM Andy Watts
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1951
x375

asked: Nov 16, 2009 at 12:34 PM

Seen: 1569 times

Last Updated: Nov 16, 2009 at 01:33 PM