Skip to content

Commit d37ec2b

Browse files
committed
Added README to TCC sample
1 parent a70c6a5 commit d37ec2b

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

  • samples/features/sql-clr/TransitiveClosure
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Implementing Transitive Closure Clustering in SQL Server using CLR UDF
2+
SQL Database don't have built-in support for transitive closure clustering, so the only workaround is to implement this algorithm in .Net framework and expose it as T-SQL function.
3+
This code sample demonstrates how to create CLR User-Defined aggregate that implements clustering.
4+
5+
### Contents
6+
7+
[About this sample](#about-this-sample)<br/>
8+
[Build the CLR/RegEx functions](#build-functions)<br/>
9+
[Add RegEx functions to your SQL database](#add-functions)<br/>
10+
[Test the functions](#test)<br/>
11+
[Disclaimers](#disclaimers)<br/>
12+
13+
<a name=about-this-sample></a>
14+
15+
## About this sample
16+
1. **Applies to:** SQL Server 2016+ Enterprise / Developer / Evaluation Edition
17+
2. **Key features:**
18+
- CLR, JSON
19+
3. **Programming Language:** .NET C#
20+
4. **Author:** Davide Mauri, Jovan Popovic [jovanpop-msft]
21+
22+
<a name=build-functions></a>
23+
24+
## Build the CLR/RegEx functions
25+
26+
1. Download the source code and open the solution using Visual Studio.
27+
2. Change the password in .pfk file and rebuild the solution in **Release** mode.
28+
3. Open and save TransitiveClosure.tt to generate output T-SQL file that will contain script that inserts .dll file with the Transitive closure clustering aggregate.
29+
30+
<a name=add-functions></a>
31+
## Add Clustering aggregate to your SQL database
32+
33+
File TransitiveClosure.sql contains the code that will import aggregate into SQL Database.
34+
35+
If you have not added CLR assemblies in your database, you should use the following script to enable CLR:
36+
```
37+
sp_configure @configname=clr_enabled, @configvalue=1
38+
GO
39+
RECONFIGURE
40+
GO
41+
```
42+
43+
Once you enable CLR, you can use the T-SQL script to add the clustering aggregate. The script depends on the location where you have built the project, and might look like:
44+
```
45+
CREATE ASSEMBLY TransitiveClosure FROM 'D:\GitHub\sql-server-samples\samples\features\sql-clr\TransitiveClosure\bin\Release\TransitiveClosureAggregatorLibrary.dll' WITH PERMISSION_SET = SAFE;
46+
GO
47+
48+
CREATE SCHEMA TC;
49+
GO
50+
51+
CREATE AGGREGATE TC.CLUSTERING(@id1 INT, @id2 INT)
52+
RETURNS NVARCHAR(MAX)
53+
EXTERNAL NAME TransitiveClosure.[TransitiveClosure.Aggregate];
54+
```
55+
56+
This code will import assembly in SQL Database and add three functions that provide clustering functionalities.
57+
58+
<a name=test></a>
59+
60+
## Test the function
61+
62+
Once you create the assembly and expose the functions, you can use it to cluster some relational data in T-SQL code:
63+
64+
```
65+
declare @edges table(n1 int, n2 int);
66+
67+
insert into @edges
68+
values (1,2),(2,3),(3,4),(4,5),(2,21),(2,22),
69+
              (7,8),(8,9),(9,10);
70+
71+
select TC.CLUSTERING(n1,n2)
72+
from @edges;
73+
```
74+
75+
<a name=disclaimers></a>
76+
77+
## Disclaimers
78+
The code included in this sample is not intended to be a set of best practices on how to build scalable enterprise grade applications. This is beyond the scope of this sample.
79+

0 commit comments

Comments
 (0)