Levenshtein distance in T-SQL

后端 未结 6 660
Happy的楠姐
Happy的楠姐 2020-11-22 06:30

I am interested in algorithm in T-SQL calculating Levenshtein distance.

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 07:02

    IIRC, with SQL Server 2005 and later you can write stored procedures in any .NET language: Using CLR Integration in SQL Server 2005. With that it shouldn't be hard to write a procedure for calculating Levenstein distance.

    A simple Hello, World! extracted from the help:

    using System;
    using System.Data;
    using Microsoft.SqlServer.Server;
    using System.Data.SqlTypes;
    
    public class HelloWorldProc
    {
        [Microsoft.SqlServer.Server.SqlProcedure]
        public static void HelloWorld(out string text)
        {
            SqlContext.Pipe.Send("Hello world!" + Environment.NewLine);
            text = "Hello world!";
        }
    }
    

    Then in your SQL Server run the following:

    CREATE ASSEMBLY helloworld from 'c:\helloworld.dll' WITH PERMISSION_SET = SAFE
    
    CREATE PROCEDURE hello
    @i nchar(25) OUTPUT
    AS
    EXTERNAL NAME helloworld.HelloWorldProc.HelloWorld
    

    And now you can test run it:

    DECLARE @J nchar(25)
    EXEC hello @J out
    PRINT @J
    

    Hope this helps.

提交回复
热议问题