Solved

Export Flat File from MS SQL

Posted on 2004-10-12
2
1,280 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TypeError: Users.save is not a function 3 60
best datatype for oracle table email creation 8 55
SQL SELECT query help 7 56
Merging spreadsheets 8 60
This article describes some very basic things about SQL Server filegroups.
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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…

860 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