Solved

Export Flat File from MS SQL

Posted on 2004-10-12
2
1,292 Views
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!!
0
Comment
Question by:shuwenjing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Expert Comment

by:Tdl
ID: 12295678
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?

Marc
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 250 total points
ID: 12296391
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.



Alan

<%@ Language=VBScript %>

<HTML>
<HEAD>
</HEAD>
<BODY>

<P>&nbsp;</P>

</BODY>
</HTML>
    <%
     
      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
          .MoveNext
        Wend
       
        ' replace vblf with break so I can view the output in browser
        ' Response.Write(Replace(strTemp,vblf,"<br />"))
       
        .Close
      End With
      objConn.Close
      Set objConn = Nothing
      Set objRs = Nothing
     
      ' Write strTemp to File
      Set objFile = CreateTempFile
      objFile.Write(strTemp)
      objFile.Close
      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
     
      %>
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

688 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