|
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
(comments are locked)
|
|
You want BCP, something like:
Should do the trick. 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,
Nov 17 '09 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"'
Nov 17 '09 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 '09 at 04:18 AM
mrdenny
(comments are locked)
|
|
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. 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 '09 at 05:54 AM
Andy Watts
(comments are locked)
|

