-
Notifications
You must be signed in to change notification settings - Fork 5
Bulk operations with MS SQL TVP parameters
Jacek edited this page Feb 20, 2019
·
4 revisions
One of the coolest features of ADO.NET provider are table-valued parameters. They greatly simplify bulk operations. Create user defined table type, and you can pass data table as parameter, if it reflects the table type structure.
CREATE TYPE [dbo].[Tag] AS TABLE(
[postId] [int] NOT NULL,
[name] [nvarchar](50) NOT NULL
)
With SqlFun, you can use TVP-s even easier way, since you can use list of records, instead of a data table:
let updateTags: int -> Tag list -> DataContext -> unit =
sql "delete from tag where postId = @id;
insert into tag (postId, name) select postId, name from @tags"
Lists, that are parts of some record, can be used too:
let updateTags: Post -> DataContext -> unit =
sql "delete from tag where postId = @id;
insert into tag (postId, name) select postId, name from @tags"
To make this feature available, you have to define sql and storedproc functions using defaults from SqlFun.MsSql instead of SqlFun.Queries:
let createConnection () = new SqlConnection(connectionString)
let sql command = sql createConnection None MsSql.defaultParamBuilder defaultRowBuilder command
let storedproc procName = storedproc createConnection None MsSql.defaultParamBuilder defaultRowBuilder command