Search code examples
f#type-providers

FSharp.Data.SqlClient Type Provider with User Defined table types


I'm using the FSharp.Data.SqlClient type provider and keep receiving errors when accessing a stored procedure that has a Table Valued parameters.

I tried the example from the documentation verbatim

CREATE TYPE myTableType AS TABLE (myId int not null, myName nvarchar(30) null) 
GO 
CREATE PROCEDURE myProc 
   @p1 myTableType readonly 
AS 
BEGIN 
   SELECT myName from @p1 p 
END 

Which should allow me to write the following

type myType = AdventureWorks2012.``User-Defined Table Types``.MyTableType

But it is not returning any options under User-Defined Table Types

This is keeping me from being able to call any stored procedures that take a Table Valued Parameter.

I'm running Microsoft SQL Server Express (64-bit) Version 12.0.2000.8 and version 1.6.2 (current latest) of the type provider


Solution

  • This is what worked for me (probably doc was not updated yet):

    [<Literal>]
    let connectionString = @"... My connection string here..."
    
    type TableValuedSample = SqlProgrammabilityProvider<connectionString>
    type myType = TableValuedSample.dbo.``User-Defined Table Types``.myTableType
    let m = [ 
        myType(myId = 2, myName = "Mike")
        myType(myId = 1, myName = "Pete") 
    ]
    let myArray = 
        (new TableValuedSample.dbo.myProc()).AsyncExecute(m) // syntax difference
        |> Async.RunSynchronously 
        |> Array.ofSeq
    
    let myRes = myArray.[0]
    match myRes with
        | Some res -> res
        | None -> ""
    

    Result:

    >
    val myArray : Option<string> [] = [|Some "Mike"; Some "Pete"|]
    val myRes : Option<string> = Some "Mike"
    val it : string = "Mike"
    

    I'm using MSSQL 2014 and 1.6.2 provider.