Include Fastenshtein in Microsoft SQL Server (SQLCLR)
masterYou can use Fastenshtein as a CLR Scalar-Valued Function within SQL Server to perform fast Levenshtein calculations directly in your SQL queries.
1. Enable CLR Integration
Run the following to enable CLR on your server:
sp_configure 'clr enabled', 1
RECONFIGURE2. Configure Security (SQL Server 2017+)
If using SQL Server 2017 (14.x) or later, you must either configure CLR strict security or disable it:
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'clr strict security', 0;
RECONFIGURE;3. Load the Assembly
Fastenshtein requires the .NET Framework version 4.6.2. You can load it using one of two methods:
Method A: Using Assembly Bits (Hex)
Download the "Fastenshtein SQL Assembly Hex" from the latest release. Copy the contents of Fastenshtein_net462.hex into the following command:
CREATE ASSEMBLY FastenshteinAssembly
FROM 0x{contents of Fastenshtein_net462.hex}
WITH PERMISSION_SET = SAFE;Method B: Using a Local Path or Network Location
Place Fastenshtein.dll (compiled in Release config or extracted from the NuGet package's lib\net462 folder) in a directory accessible by SQL Server:
CREATE ASSEMBLY FastenshteinAssembly FROM 'C:\Fastenshtein.dll' WITH PERMISSION_SET = SAFE4. Create the SQL Function
Once the assembly is loaded, create the [Levenshtein] function:
CREATE FUNCTION [Levenshtein](@value1 [nvarchar](MAX), @value2 [nvarchar](MAX))
RETURNS [int]
AS
EXTERNAL NAME [FastenshteinAssembly].[Fastenshtein.Levenshtein].[Distance]
GO5. Usage
DECLARE @retVal AS INTEGER
SELECT @retVal = [dbo].[Levenshtein]('Test','test')
SELECT @retVal