Write to a text file from Stored Procedure

They have: 82 posts

Joined: Oct 2001

I need to write the results of a select statement
to a text file. I am thinking I can call an object
from the procedure and write the file. Before I go
through all of this trouble does anyone have any better
ideas? Is it possible just to do this from the select
statement?

Thanks,

Mike

Blessed is the man who fears the LORD, who delights greatly in his commandments. Psalms 112:1

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

If you have access to Extended Stored Procs (xp) you can use xp_cmdshell to gain access to the command line and do whatever you want, or just send control back to the web server and create the file there.

PJ | Are we there yet?
pjboettcher.com

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

It might help knowing what DB and Server you are running. Peter gives you the how to in Windows. On a 'nix box using MySQL at the shell, the command would be:

<?php
SELECT
* from table
WHERE fieldname
='value'
INTO outfile \"/home/username/filename.txt\";
?>

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

They have: 82 posts

Joined: Oct 2001

Sorry guys

I am using SQL Server 2000

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.