0% found this document useful (0 votes)
188 views

DLL in SQL

The document describes how to install and use the Double Metaphone phonetic encoding algorithm in SQL Server. It includes steps to: 1) Create an assembly from the Double Metaphone DLL file. 2) Create a user-defined type (UDT) and functions (UDFs) to encode strings and compare encodings. 3) Populate a sample table with surname encodings for matching. 4) Provide an example stored procedure to encode trade names and allow searching/matching on the encodings.

Uploaded by

ganeshkumars
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
188 views

DLL in SQL

The document describes how to install and use the Double Metaphone phonetic encoding algorithm in SQL Server. It includes steps to: 1) Create an assembly from the Double Metaphone DLL file. 2) Create a user-defined type (UDT) and functions (UDFs) to encode strings and compare encodings. 3) Populate a sample table with surname encodings for matching. 4) Provide an example stored procedure to encode trade names and allow searching/matching on the encodings.

Uploaded by

ganeshkumars
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Step 1: Create Assembly

CREATE ASSEMBLY DoubleMetaphone


FROM 'C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Binn\DoubleMetaphone.dll'
GO

-- Here we install the DoubleMetaphoneResult UDT

CREATE TYPE DoubleMetaphoneResult


EXTERNAL NAME DoubleMetaphone.[Phonetic.Tools.DoubleMetaphoneResult]
GO

-- Here we install the DoubleMetaphoneCompare() UDF

CREATE FUNCTION DoubleMetaphoneCompare (@r1 DoubleMetaphoneResult, @r2


DoubleMetaphoneResult)
RETURNS Integer
AS
EXTERNAL NAME DoubleMetaphone.
[Phonetic.Tools.DoubleMetaphone].DoubleMetaphoneCompare
GO

-- And finally we install the DoubleMetaphoneEncode UDF

CREATE FUNCTION DoubleMetaphoneEncode (@string NVARCHAR(256))


RETURNS DoubleMetaphoneResult
AS
EXTERNAL NAME DoubleMetaphone.
[Phonetic.Tools.DoubleMetaphone].DoubleMetaphoneEncode
GO

-- Just to make sure the "clr enabled" option is configured


-- correctly

sp_configure "clr enabled", 1


go
reconfigure
go
EXAMPLE

1. Create_People_Table.sql

-- Create simple demo table


CREATE TABLE People
(
Id INTEGER IDENTITY (1, 1) NOT NULL PRIMARY KEY,
Surname NVARCHAR(32) NOT NULL,
DMEncoding DoubleMetaphoneResult
)
GO

-- Insert surnames

INSERT INTO People (Surname) VALUES (N'AAGAARD')


INSERT INTO People (Surname) VALUES (N'AARON')
INSERT INTO People (Surname) VALUES (N'ABBE')
INSERT INTO People (Surname) VALUES (N'ABBETT')
INSERT INTO People (Surname) VALUES (N'ABBEY')
INSERT INTO People (Surname) VALUES (N'ABBOTT')
INSERT INTO People (Surname) VALUES (N'ABEL')
INSERT INTO People (Surname) VALUES (N'ABELL')
INSERT INTO People (Surname) VALUES (N'ABERNATHY')
INSERT INTO People (Surname) VALUES (N'ABRAHAMS')
INSERT INTO People (Surname) VALUES (N'ABRAHAMSON')
INSERT INTO People (Surname) VALUES (N'ABRAM')
INSERT INTO People (Surname) VALUES (N'ABSHIRE')
INSERT INTO People (Surname) VALUES (N'ABT')
INSERT INTO People (Surname) VALUES (N'ACE')
INSERT INTO People (Surname) VALUES (N'ACHATZ')
GO

2. declared one column as the DoubleMetaphoneResult

-- Here we store the Double Metaphone encodings in the


-- same table. Notice that we've declared one column
-- as the DoubleMetaphoneResult UDT type.

UPDATE People SET DMEncoding = dbo.DoubleMetaphoneEncode(Surname)


GO

SELECT * FROM People


GO
3. Try this query

-- Here we declare a DoubleMetaphoneResult UDT variable


-- and encode 'ATKIN'. You can encode any name you want
-- here, just replace 'ATKIN' with the name of your
-- choice.

DECLARE @r DoubleMetaphoneResult
SET @r = dbo.DoubleMetaphoneEncode (N'knew')

-- Here we are selecting only those matches with a score >= 1


-- which is the weakest match. You can raise this value to
-- >= 2 for a medium to strong match or = 3 for only the best
-- matches.
--
-- We are using the previously created People table for this
-- query.

SELECT dbo.DoubleMetaphoneCompare(DMEncoding, @r) AS SCORE, *


FROM People
WHERE dbo.DoubleMetaphoneCompare(DMEncoding, @r) >= 1
ORDER BY SCORE DESC, SURNAME ASC
IN your case

Step 1 : Create one sp for you

Create procedure getDoubleMetaphoneResult()


{
arg1 nvarchar(100)
AS
Begin
DECLARE @r DoubleMetaphoneResult
SET @r = dbo.DoubleMetaphoneEncode (arg1)

create table #tmp


{
TradeNo int,
TradeName varchar(100),
TradeClass varchar(100),
FilingDate varchar(100),
PropName varchar(100),
PropAddress varchar(100),
AgentAddress varchar(100),
GoodsandServices varchar(100),
UserClaimDate varchar(100),
Jurisdiction varchar(100),
TradeImage varchar(100),
TradeImgName varchar(100),
DMEncoding DoubleMetaphoneResult //This is new column
}

insert into #tmp(TradeNo, TradeName, TradeClass,


FilingDate, PropName, PropAddress, AgentAddress,
GoodsandServices, UserClaimDate, Jurisdiction,
TradeImage, TradeImgName, DMEncoding)
select TradeNo, TradeName, TradeClass,
FilingDate, PropName, PropAddress, AgentAddress,
GoodsandServices, UserClaimDate, Jurisdiction,
TradeImage, TradeImgName,
dbo.DoubleMetaphoneEncode(TradeName) as DMEncoding from
Tradetable

Select * from #tmp where WHERE dbo.DoubleMetaphoneCompare(DMEncoding,


@r) >= 1

End

You might also like