Search code examples
sqlsql-serverf#type-providers

F#: Trouble with loading data into SQL Server


I am new to programming and F# is my first language.

I want to load data into a SQL Server database using F# code. Here is the table that is supposed to store my data:

CREATE TABLE [dbo].[Scores](
    [EventName] [nvarchar](100) NOT NULL,
    [Winner] [nvarchar](50) NOT NULL,
    [Loser] [nvarchar](50) NOT NULL,
    [Score1] [nvarchar](10) NOT NULL,
    [Score2] [nvarchar](10) NOT NULL,
    [Score3] [nvarchar](10) NOT NULL
) ON [PRIMARY]

Score1, Score2 and Score3 are supposed to contain judges' scores, which might be unavailable sometimes.

Here is my F# code for loading the data:

            new dbSchema.ServiceTypes.Fights(EventName = fightSummary.event,
                                            Winner = fightSummary.winner.Value,
                                            Loser = fightSummary.loser.Value,

                                            Score1 = if judgesScoresExist then
                                                        fightSummary.judgesScores.Value.[0]
                                                     else
                                                        "None",
                                            Score2 = if judgesScoresExist then
                                                        fightSummary.judgesScores.Value.[1]
                                                     else 
                                                        "None",
                                            Score3 = if judgesScoresExist then
                                                        fightSummary.judgesScores.Value.[2]
                                                     else "None")

When I try to run the F# code above, I receive the following error message:

This expression was expected to have type string but here has type 'a * 'b

Apparently, the compiler interprets the part

"None", Score2 = ...

as a tuple.

I tried searching online for a solution, but have yet to find one.

What changes should I make so that I can load my data into SQL Server?


Solution

  • Put if then else expressions in parenthesis:

    let v = new Fights(EventName = fightSummary.event,
                       Winner = fightSummary.winner.Value,
                       Loser = fightSummary.loser.Value,
    
                       Score1 = (if judgesScoresExist then
                                   fightSummary.judgesScores.Value.[0]
                                 else
                                   "None"),
                        ...) // etc.