Export MS SQL 7 Server tables having TEXT fields to an ASCII file

Posted on 2004-11-12
Last Modified: 2008-03-03
We are developing an application that uses MS SQL 7 database. The database contains 100-500 thousand records. The application needs to be able to save the database to a text file on user request and we also have to be able to reproduce the database from the saved text file (kind of syncronisation on the client).

The database contains TEXT fields as well. We are looking for a way to export the database to the text file.

The problem is that the TEXT fields can contain any type of characters. So we don't think we can use the BCP tool because it's not possible to define any delimiter that does not appear for sure in the TEXT field.

We were also thinking of solutions like replacing all " characters to "" in  the TEXT field and put a " in the beginning and the end of the field but there does not seem to be any REPLACE function for TEXT fields to accomplish this.

Iterating through the table records and fields and exporting them one by one seems to be the only solution but it's not too fast.

Do you have experience with this problem?

Question by:brevai
    LVL 19

    Expert Comment

    by:Melih SARICA

    why dont u use a specific delimeter for Example <rowdelimeter> or <fielddelimeter>

    bcp msdb.dbo.sysjobsteps out "c:\test.txt" -c -U"Username" -P"password" -t"<fielddelimeter>" -r"<rowdelimeter>"

    Melih sARICa

    Author Comment

    Because the table also contains TEXT fields (containing for example word documents) and we can't assume that the delimiter we chose won't occur in the TEXT field. And from then on the exported table's structure is messed up in the exported file and it can't be imported anymore.

    LVL 19

    Expert Comment

    by:Melih SARICA
    if i were  u i would try to use a guid for a field delimeter

    everytime u send data send a format file with text data

    create a GUID for each data Export.. it cant be produced anywhere else and again..
    so it ll protect u

    If  think that its not the solution

    u must copy data to an Access file and then Send it to ur customer

    Melih SARICA

    LVL 26

    Expert Comment

    by:Alan Warren
    Hi Balazs

    have you considered exporting as XML?

    Persisting an ADO Recordset to XML



    Author Comment

    Hi Alan,

    The solution you have provided for exporting the database works fine on the server. We are exporting the database to XML tables.

    Now our next problem is that we need to import the database on the client.

    Currently we are using an MS Access database on the client. We have managed to load the XML file into a recordset using the LoadFromFile method. But then we don't manage to save the records to a table without looping through them. Do you have a solution for that?

    We are also considering to move to SQLite on the client. Any solutions here?

    LVL 26

    Accepted Solution

    Hi Balazs

    I don't have a solution other the one you mentioned, I believe sql server has some better ways of doing this, with things such as the For XML clause, not done much in this area though.

    The following is how I parse ADO - persited XML into a Jet catalog.

    ' The main thing we have going for us that the field names and types in the XML
    '  are the same as the field names and types in the target table
    ' If they aren't then it is the old steam method :(

    ' Instantite an ADO recordset for the target table
    ' Instantiate an ADO recordset from the source XML

    ' Loop through

    With rs
      For Each fld In rsEmail.Fields
        'ProgCtrl_One.Value = ProgCtrl_One.Value + 1
        Dim sFieldName As String
        sFieldName = fld.Name
        If fld.Value = "" Then
            .Fields(fld.Name).Value = Null
            .Fields(fld.Name).Value = fld.Value
        End If
        Next fld
    End With

    Re: Sql server and XML:

    Success with your app!


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now