Writing to text files within a SQL Stored Proc

  • From within a SQL Server Stored Procedure, is it possible to read through a cursor writing each record to a unique text file whose name is dynamically generated based on a field within the cursor record retrieved?

    Please Help!

    Thank You

  • Yes you have to use the OA ole creation method to open filesystem objects

    see BOL

    quote:


    How to create an OLE Automation object (Transact-SQL)

    To create an OLE Automation object

    Call sp_OACreate to create the object.

    Use the object.

    Call sp_OAGetProperty to get a property value.

    Call sp_OASetProperty to set a property to a new value.

    Call sp_OAMethod to call a method.

    Call sp_OAGetErrorInfo to get the most recent error information.

    Call sp_OADestroy to destroy the object.


  • Two other ways...

    xp_cmdshell using echo >> (the double greater than sign appends). For instance:

    xp_cmdshell 'echo Write to a text file >> c:\temp\test.txt'

    Second, is using Jet and a linked server connection. However, there is some prep work (see sp_addlinkedserver in BOL).

    K. Brian Kelley, GSEC

    http://www.truthsolutions.com/

    Author: Start to Finish Guide to SQL Server Performance Monitoring

    http://www.netimpress.com/

    K. Brian Kelley
    @kbriankelley

  • Just did some poking around and I am getting an ole automation error message on the example I did.

    OLE Automation Error Information

    HRESULT: 0x8002801d

    Source: ODSOLE Extended Procedure

    Description: Library not registered.

    What library? Running SQL2000, sp3a

    Thanks,

    Chris

  • quote:


    Just did some poking around and I am getting an ole automation error message on the example I did.

    OLE Automation Error Information

    HRESULT: 0x8002801d

    Source: ODSOLE Extended Procedure

    Description: Library not registered.

    What library? Running SQL2000, sp3a

    Thanks,

    Chris


    Can you post what you did?

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

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