output to a text file

Is it possible to send the output of a query to a text file in a stored procedure When I run stored procedure in Query Analyzer I am able to do that and I am wondering if this is possible in a automated way




Answer this question

output to a text file

  • Alexey Vishnyakov

    No,, there is no query command for that. What you can do is to call the xp_cmdshell procedure to call the OSQL program at the command prompt. With using the -Q parameter you can pass the query you want to execute, the -O parameter will determine the output direction for you resultset.

    HTH, Jens SUessmeyer.

    ---
    http://www.sqlserver2005.de
    ---

  • Zooz

    Hi,

    you can call the bcp utility from stored procedure..

    create procedure copytoTextFile as
    exec master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout c:\Authors.txt -c -S -Usa -Ptest'




  • Jehan Badshah

    by using DTS you can do it.

  • Tryin2Bgood

    This will work if xp_cmdshell is enabled

    DECLARE @isqlString varchar(255)

    SELECT @isqlString = 'isql -Q "SELECT top 10 FROM TABLE" -E -o C:\Results.txt'

    EXEC master..xp_cmdshell @isqlString



  • output to a text file