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 7067821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:14:53+00:00 2026-05-28T05:14:53+00:00

In the following code, I would like to insert or update rows in an

  • 0

In the following code, I would like to insert or update rows in an SQL table through F#.
It takes a matrix of tuple representing users and an associated scores (usrID,score) which are the results of some F# calculations.
Now I want to update a SQL table called UserScoresTable.
The code I wrote is working but is very slow.

let cnn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings.Item("Database").ConnectionString)
cnn.Open()

// Definition d'une fonction qui execute une demande sous MySQL
let execNonQuery s =
    let comm = new System.Data.SqlClient.SqlCommand(s, cnn, CommandTimeout = 10)
    comm.ExecuteNonQuery() |> ignore

// Definition d'une fonction qui utilise les éléments d'une matrice pour updater une table dans MySQL
let updateMySQLTable (m : Matrix<float * float>) =
    for j in 0 .. (snd m.Dimensions) - 1 do
        for i in 1 .. (fst m.Dimensions) - 1 do
        // first check if the user and score and date do not exist
            sprintf "IF NOT EXISTS (SELECT * FROM ProductScores WHERE UserID = %f AND ProductID = %f) INSERT INTO ProductScores (UserID, Score, Date) VALUES (%f, %f,CURRENT_TIMESTAMP)"  (fst m.[i, j]) (snd m.[i, j])
            |>  execNonQuery
       // otherwise update the row
            sprintf "UPDATE ProductScores SET Score = %f, Date = CURRENT_TIMESTAMP WHERE UserID = %f " (snd m.[i, j]) (fst m.[i, j])
            |>  execNonQuery

I would like to avoid the two execNonQuery requests in the code by using a stored procedure such as

CREATE PROCEDURE updateScoreByUsers 
    -- Add the parameters for the stored procedure here
    @UserID int, 
    @Score  float

AS
BEGIN

    IF NOT EXISTS 
    (SELECT * FROM ProductScores WHERE UserID = @UserID ) 
        INSERT INTO 
        ProductScores (UserID, Score, Date) VALUES (@UserID,@Score,CURRENT_TIMESTAMP)
    ELSE

    UPDATE ProductScores 
        SET Score = @Score, Date = CURRENT_TIMESTAMP 
        WHERE UserID = @UserID 
END
GO

But I don’t know how to call a SQL stored procedure in F#.
How can I call it with F#?
DO you see any other way of improvements?

  • 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-28T05:14:53+00:00Added an answer on May 28, 2026 at 5:14 am

    Set SqlCommand.CommandText to the name of the stored procedure and set CommandType to StoredProcedure. For example

    use cmd = new SqlCommand("MyStoredProcedure", con)
    cmd.CommandType <- CommandType.StoredProcedure
    cmd.ExecuteNonQuery()
    

    If you’re on 2008 you may want to look into the MERGE statement.

    To significantly improve performance you need to avoid making a database call for each item. Perhaps use SqlBulkCopy to load all the data to the server at once (use a temp table or designated staging table), then use set-based operations (UPDATE/INSERT/MERGE) to merge with your target table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to insert the following code: if ($this->id == null) { return
In the following code I would like the div with 'y' to match the
I have the following code, which works as I expect. What I would like
I have the following html code: <h3 id=headerid><span onclick=expandCollapse('headerid')>&uArr;</span>Header title</h3> I would like to
I have the following snippet where I would like to extract code between the
I would like to do something like the following: def add(a, b): #some code
I have code, similar to the following, that I would like to modify: Sub
I have the following code: public function createNewGuide($userID,$guideName) { $sql = INSERT INTO myTable(name,
I would like someone to modify the following code to save data to generate
I'm curious if following code would be considered safe? using (SqlConnection cn = new

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.