Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 495445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:35:38+00:00 2026-05-13T05:35:38+00:00

I have recently been learning F# and functional programming. One application I have found

  • 0

I have recently been learning F# and functional programming. One application I have found very useful is generating SQL inserts for a data load from a CSV (or excel table) with some associated ID’s.

The following code is my result, which I believe I will find very handy in the future. I thought others could also benefit from this and I welcome suggestions and other scripts which people have found invaluable in their collection:

// Returns some dataload SQL for area mapping.
open System
open System.IO

// Read and split CSV into lists
let map_ngo_area = File.ReadAllLines(@"P:\MY_TABLE.csv") 
                        |> Array.to_list
                        |> List.map(fun x -> (x.Split([|','|]) 
                                                |> Array.map(fun y -> y.Trim())) 
                                                |> Array.to_list)

// Output Formatting function
let format_sql_record = "INSERT INTO MyTable
     (ID, REF1_ID, REF2_ID, CreatedUser, CreatedDateTime, LastModifiedUser, LastModifiedDateTime)
     VALUES
     ( {0}, {1}, {2}, 'system', getDate(), 'system', getDate() )"

 // Generate the SQL for the given list.  
let generate_sql list = list |> List.mapi(fun index row -> 
                                            match row with
                                                | [ngo_id; area_id] -> String.Format(format_sql_record, ((int index)+1), ngo_id, area_id) |> printfn "%s"
                                                | _ -> printfn "")

// Main Execution
map_ngo_area |> generate_sql |> ignore

// End of program, pause to view console output.
System.Console.ReadKey() |> ignore

Any suggestions on improving my F# code or process? Comments also welcome, as I am fairly new at this paradigm and shifting thinking is not as forthcoming as I expected.

Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:35:39+00:00Added an answer on May 13, 2026 at 5:35 am

    Here are a few suggestions:

    • Don’t use List.mapi with functions that return unit, since there’s not much you can do with the resulting unit list. You should use List.iteri instead, which will allow you to omit the |> ignore at the end of your main execution section.
    • Going further, rather than having generate_sql print the generated lines one at a time, it might be better to generate a list of strings instead. Note that in this case, you would go back to using List.mapi since the function that you apply would return a result for each line.
    • Use F#’s print formatting rather than String.Format. For instance rather than having format_sql_record be a string, have it be a function of type int->string->string->string: let format_sql_record = sprintf "INSERT INTO ... VALUES (%i, %s, %s, ...)"
    • Depending on the version of F# that you’re using, you should probably be using the renamed function Array.toList instead of Array.to_list, since that’s the name that will be used in the final release of F#.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from classes object orientation languages and recently I have been learning those
I've been a web developer for some time now, and have recently started learning
I have recently started learning Perl and one of my latest assignments involves searching
I have recently been learning about doctypes, and was I wondering what the differences
I have recently been learning more about design patterns and thought I'd take a
I have recently been using junit in eclipse and I am still learning. I
Have recently been given a project to complete which uses XML quite extensively.Am looking
I have recently been doing a bit of investigation into the different types of
I have recently been thinking about the difference between the two ways of defining
I have recently been working with Python using Komodo Edit and other simpler editors

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.