From 4112449f48ad2202e504d5950573c1af89907f83 Mon Sep 17 00:00:00 2001 From: Jacek Date: Sat, 7 Dec 2019 00:27:28 +0100 Subject: [PATCH] Create Bulk-operations-with-MS-SQL-TVP-parameters.md --- ...k-operations-with-MS-SQL-TVP-parameters.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 docs/Bulk-operations-with-MS-SQL-TVP-parameters.md diff --git a/docs/Bulk-operations-with-MS-SQL-TVP-parameters.md b/docs/Bulk-operations-with-MS-SQL-TVP-parameters.md new file mode 100644 index 0000000..86fb3c3 --- /dev/null +++ b/docs/Bulk-operations-with-MS-SQL-TVP-parameters.md @@ -0,0 +1,31 @@ +# Bulk operations with MS SQL TVP parameters + +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. +```sql +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: +```fsharp +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: +```fsharp +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: +```fsharp +let createConnection () = new SqlConnection(connectionString) +let generatorConfig = MsSql.createDefaultConfig createConnection + +let sql commandText = sql generatorConfig commandText + +let proc procName = proc generatorConfig procName +```