How to write the query results to a text file

  • Hi,

    Request your help with this.

    The table RACCRUALDETAIL contains several records for each PERSONNUM. I need to run this query once for each PERSONNUM and store the results(comma separated) to a text file each time. If there are no results, there should still be a row which contains the amount as 0.

    SELECT ACCRUALCODENAME, EFFECTIVEDATE, AMOUNT, PERSONNUM

    FROM RACCRUALDETAIL

    WHERE

    (PERSONNUM = '990031084')

    AND ((ACCRUALCODENAME = 'Vacation') )

    AND (TYPE = 11)

    AND EFFECTIVEDATE =

    (

    select max(EFFECTIVEDATE) FROM VP_RACCRUALDETAIL

    WHERE (PERSONNUM = '990031084')

    AND ((ACCRUALCODENAME = 'Vacation') )

    AND (TYPE = 11)

    )

    Thanks,

    Krish.

  • Hi Krish,

    have a look at SSIS - very easy to do it using that

    I'm a DBA.
    I'm not paid to solve problems. I'm paid to prevent them.

  • Hi Krish

    If you are not familair with SSIS, you can use BCP to export query results to a csv file. I would recommend to have a script containing your sql query and run that script using BCP. you can run BCP from cmd.exe or can use xp_cmdshell. But you must familiarise yourself with security issues related to using xp_cmdshell.

    you can find more details about BCP at http://msdn.microsoft.com/en-us/library/ms162802.aspx

    Cheers

    Anam

  • Another option is SQLCMD.

    example

    sqlcmd -S "servername" -Q "some query" -o \\Output_location\outputfile.txt

    there are plenty other options.

    -- Cory

  • Just use Import EXport wizard.

Viewing 5 posts - 1 through 4 (of 4 total)

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