[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Export Flat File from MS SQL

Posted on 2004-10-12
2
Medium Priority
?
1,301 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 1000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

650 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