Sei sulla pagina 1di 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

Potrebbero piacerti anche