Export Flat File from MS SQL

Posted on 2004-10-12
Last Modified: 2012-06-27
I got a job that request me to submit a flat file to a bank. this flat file using the EDIFactoring format for IDEAL Account Receivable System. and the file must have a fixed format, eg  to start a record:

Position 1-5    RecordID   Lenth 5   Value='ABC01'
Position 6-19  Filter         Lenth 14 Value is space

I only know a flat file is a one dimemsion data file can be stored as a .txt file.

May I know if it's possible to use asp to generate such a file? If possible how to ? Thank you very much!!
Question by:shuwenjing

Expert Comment

Comment Utility
Hi, it sure is possible to use asp for it, you can also use most other languages like vb, c++ and this can even be done directly in the database. But what are your requirements? Do you have to do it in asp? Or would you like to do it in asp because you know asp? Are you allowed to do it in the database?

LVL 26

Accepted Solution

Alan Warren earned 250 total points
Comment Utility
Hi shuwenjing

You will need something like this to persist a  recordset as a text file using ASP
This example connects to Northwind sql catalog and pulls 2 fields from the employees table
and space pads the fields then uses the FileSystemObject to create a temporary file to persist the data.


<%@ Language=VBScript %>



      Dim objConn                   ' Database Connection
      dim sDataConnect              ' ADO SQL connect string
      Dim objRS                     ' ADO Recordset Object
      Dim objFso                    ' Scripting.FileSystem object
      Dim strSQL
      Dim strTemp
      Dim objFile                   ' Temporary file
      ' Instantiate a database connection
      Set objConn = Server.CreateObject("ADODB.Connection")

      ' Create an ADO connect string
      sDataConnect = adoConnectSQL("password", "sa", "NorthWind", "(local)")
      ' Open a database connection
      objConn.Open sDataConnect
      ' Build your sql string
      strSQL = "SELECT EmployeeID, LastName FROM dbo.Employees"
      ' Instantiate your recordset object
      Set objRs = Server.CreateObject("ADODB.Recordset")
      ' Open the recordset
      objRS.Open strSQL, objConn,3,3
      ' banking files have fields padded with space characters
      ' space chars differ from empty space "   "
      ' either left or right padding is specified
        ' Example of left padding
        ' Space(5-Len(.Fields("EmployeeID"))) & .Fields("EmployeeID")
        ' Space(14-Len(.Fields("LastName"))) & .Fields("LastName")
        ' Example of Right padding
        ' .Fields("EmployeeID") & Space(5-Len(.Fields("EmployeeID")))
        ' .Fields("LastName") & Space(14-Len(.Fields("LastName")))
        ' output like "2    Fuller        "
      With objRs
        While Not .EOF
          strTemp = strTemp & .Fields("EmployeeID") & Space(5-Len(.Fields("EmployeeID")))
          strTemp = strTemp & .Fields("LastName") & Space(14-Len(.Fields("LastName")))
          strTemp = strTemp & vbcrlf
        ' replace vblf with break so I can view the output in browser
        ' Response.Write(Replace(strTemp,vblf,"<br />"))
      End With
      Set objConn = Nothing
      Set objRs = Nothing
      ' Write strTemp to File
      Set objFile = CreateTempFile
      Set objFile = nothing
        Function CreateTempFile
           Dim tfolder, tname, tfile, fso
           set fso = Server.CreateObject("Scripting.FileSystemObject")
           Const TemporaryFolder = 2
           Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
           tname = fso.GetTempName
           Set tfile = tfolder.CreateTextFile(tname)
           Set CreateTempFile = tfile
           Response.Write("File saved to: " & tfolder & "\" & tname )
        End Function

        Public Function adoConnectSQL(psPassword, psUser, psCatalog, psDataSource)

          ' Returns SQLOLEDB ADO connect string
          ' Uses SQL Server security
          ' web-farm server expects: user = 'anonymous' and password=""
          ' anonymous user has very limited rights

          Dim sProvider, sPassword, sPersist, sUser, sCatalog, sDataSource

          sProvider = "Provider=SQLOLEDB.1;"
          sPassword = "Password=" & psPassword & ";"
          sPersist = "Persist Security Info=True;"
          sUser = "User ID=" & psUser & ";"
          sCatalog = "Initial Catalog=" & psCatalog & ";"
          sDataSource = "Data Source=" & psDataSource
          adoConnectSQL = sProvider & sPassword & sPersist & sUser & sCatalog & sDataSource

        End Function

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now