473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Output an SQL Query to a text file

Hi,

How would I go about writing the results of an SQL query to a text
file?

I cannot find any info in the Online help files.

For example, I would like the results of:

SELECT * FROM TableA

To be written to the file result.txt
Alex

Jul 23 '05 #1
10 149054
Open query analyzer. Go to Query and select Result to file.... and
during execution, it will prompt a file name....

Jul 23 '05 #2
Thanks but I cannot do that.

This must query must be invisible to the end user.

For example, if a user performs some function on my system, I wish to
execute a query on what they have selected and output the results to a
file. I do not want my user to be selecting Save As etc..

Jul 23 '05 #3
First your question is not clear. I am still not sure what you are
trying to accomplish.

First if you have a query that you dont want your user to know, wrap it
up in a stored proc and schedule it in a job and let the user execute
the job.

Another easy way is to create a dts and let the user run the dts that
should run the query and output the file.

Hope this helps..!

Jul 23 '05 #4
Ok, I have no idea what a dts is!!

Is it possible to output the results from a stored procedure to a
specified file.

I do not want to use a tool or anything, just simply write the results
from a query to a file.

Jul 23 '05 #5
JD
Alex,
You might try adding this to your connection string: -o '<path
name>\results.txt.

such as: isql -SMyserver -Uusera -Puserapassword -i mysqlquery.sql -o
results.tx

hth

"Alex" <al**********@lineone.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi,

How would I go about writing the results of an SQL query to a text
file?

I cannot find any info in the Online help files.

For example, I would like the results of:

SELECT * FROM TableA

To be written to the file result.txt
Alex

Jul 23 '05 #6
On 2/14/05 4:24 PM, in article
11**********************@z14g2000cwz.googlegroups. com, "Alex"
<al**********@lineone.net> wrote:
Ok, I have no idea what a dts is!!

Is it possible to output the results from a stored procedure to a
specified file.

I do not want to use a tool or anything, just simply write the results
from a query to a file.


There is no OUTPUT TO FILE argument in a SELECT clause. You can SELECT INTO
a temporary table but this doesn't help you. The simplest thing to do (path
of least resistance) is to use a program in the middle to handle the query
and writing the recordset into a file. For example, if you wanted to create
an .ASP web form you could simply ask for the customer's email address, run
a query and send them the results. You could write a VB app or VB script to
run locally and generate a file from your query.

-Greg

Jul 23 '05 #7
There isn't a direct method. Two possible workarounds are OSQL and
BCP. I tend to use BCP. BCP only lets you output from a permanent
table, though.

I would create a table called "CACHE" where one of the columns is "IP"
and the other cols match the SELECT query. Then store the output into
CACHE along with the user's IP. Then run BCP to output from CACHE.

Jul 23 '05 #8
Alex (al**********@lineone.net) writes:
Ok, I have no idea what a dts is!!

Is it possible to output the results from a stored procedure to a
specified file.

I do not want to use a tool or anything, just simply write the results
from a query to a file.


No, you don't want to do that. Yes, you can invoke xp_cmdshell and from
there do all sorts of things. But then your users would have to have
permission to use xp_cmdshell, and that could be a security issue.

It's not clear why you want to write to a file, but if you want to
log some stuff for debug reasons - write to a table instead. You are
in a database, not in a file system.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #9
Ok this is great, the tables I need data outputting from are permanent.

Basically it is for creating a log file. The database is huge, but the
log only needs to contain the contents of a few tables. It has to go to
a file because I want to be able to save the results of the query to
disk or print it out.

Can I BCP out from several tables into the same file?

i.e. I need SELECT * from TableA, TableB, TableC to result.txt

Security is not an issue.

Jul 23 '05 #10
Alex wrote:
Ok this is great, the tables I need data outputting from are permanent.
Basically it is for creating a log file. The database is huge, but the log only needs to contain the contents of a few tables. It has to go to a file because I want to be able to save the results of the query to
disk or print it out.

Can I BCP out from several tables into the same file?

i.e. I need SELECT * from TableA, TableB, TableC to result.txt

Security is not an issue.


It's a little involved but you can write a select statement joining
several tables. Each time BCP executes, I believe it overwrites the
text file.

Check out this article:
http://www.sqlteam.com/item.asp?ItemID=4722

Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: qbschmidt | last post by:
i can input and output to a text file as long as the file is in the same directory as my c++ program. but i cant figure out how to input/output to a file in another directory i.e. I:\c++projects...
2
by: Joe Saliba | last post by:
hi, we're trying to output a text file in other format including delimeters (; instead of , ) and changing text delimeters too any way to do it from vb ?? thx *** Sent via Developersdex...
0
by: IK | last post by:
Hi all, How can I save trace output to the text file in app's dir. In addition, I need to save debug info from Session_End event in global.asax. The code below saves file only in...
0
by: Trent USTA | last post by:
I'm having a problem with line feeds in an xslcompiledtransform in .net 2.0. I'm trying to use xsl to transform xml to a text file that can be read on a Unix box. The problem is that when I insert...
2
by: garth.waring | last post by:
Hi all I have seen the In Clause used to remotely query a text file done but cannot find the right syntax The example below is for an excel file : SELECT * FROM IN...
1
by: galivio | last post by:
I need a code in C where accepts inputs coming from an external text file and will give an output stored on another text file. For example, a C program to compute the sum of the two integers. The...
1
by: Kaitsu | last post by:
How can I get data from table to text-file by SQL-commands, for example SELECT * FROM Table. Result to text-file Table.txt.
1
by: rafnavsun | last post by:
Hi Everybody, I need to concatenate all records on one field as one line. Example: Field 1 has 50 records, record1, record2 to record50. I need to display an output in Text that read as: ...
13
by: jhamb | last post by:
Hi, This code is in Perl (just a trial, not tested) to parse a text file and output to another file. It is used to delete lines that are not required and output lines that the user wants, to a new...
0
by: grant | last post by:
I have a transfer data task that gets some data from a table and outputs it to a pipe delimitd text file. How can I configure a transfer data task so it outputs the field names from the table in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.