sending query output to a text file

  • Hi there!

    Is there an easy way to direct SQL query output to a text file? ... For instance, to run SELECT * FROM authors and send the results to a text file, ...

    I need this in T-sql only. Not in VB script

    Thanks

  • Yep... use OSQL to run the query from a CMD prompt (or using xp_CmdShell w/ trusted connection) with a redirect (the -o option) to a text file.

    See "Books Online" for the full syntax... if you run into problems with the syntax, post back and we'll help...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Hi jeff

    i tried below but can't get round it. Please help.

    DECLARE @cmd VARCHAR(5000)

     

    SET @cmd = 'OSQL -S support-ts2 -dNCL_REMOTE_SP6D '

        + ' -Usa -Pseaward'

        + ' -Q"select notes from person where len(notes) > 1000"'

        + ' -oc:\authors.txt'

     

    EXEC master..xp_cmdshell @cmd, NO_OUTPUT

    c:\authors.txt is the file i wanna write to.

    Thanks a lot mate

  • One thing you can do is this :

    DECLARE @cmd VARCHAR(5000)

     

    SET @cmd = 'OSQL -S support-ts2 -dNCL_REMOTE_SP6D '

        + ' -Usa -Pseaward'

        + ' -Q"select notes from person where len(notes) > 1000"'

        + ' -oc:\authors.txt'

    PRINT @cmd

    --my results

    OSQL -S support-ts2 -dNCL_REMOTE_SP6D -Usa -Pseaward -Q"select notes from person where len(notes) > 1000" -oc:\authors.txt

     

    then manually run that in OSQL and make corrections untill you get it working.  Then come back and fix the string.

  • Vijay,

    You can debug the query by checking the authors.txt file.. It contains the vital error information

     

    --Ramesh

    --Ramesh


  • thanks guys..

    i managed to do it...

    DECLARE @cmd VARCHAR(5000)

     

    SET @cmd = 'OSQL -S support_svr1 -d NCLREMOTE_SP6D '

        + ' -U sa -P seaward'

        + ' -Q "select notes from person where len(notes) > 1000"'

        + ' -o c:\Notes.txt'

     

    EXEC master..xp_cmdshell @cmd, NO_OUTPUT

  • BTW you might want to change your login credentials now .

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply