I have two sets of data (locations) in separate tables and I need to compare if they match or not. I have a UDF which performs a calculation based upon 5 values from each table.
How do I perform a select with a join using this udf?
my udf is basically defined by....
ALTER FUNCTION [dbo].[MatchRanking]
(
@Latitude FLOAT
, @Longitude FLOAT
, @Postcode VARCHAR(16)
, @CompanyName VARCHAR(256)
, @TelephoneNumber VARCHAR(32)
, @Latitude2 FLOAT
, @Longitude2 FLOAT
, @Postcode2 VARCHAR(16)
, @CompanyName2 VARCHAR(256)
, @TelephoneNumber2 VARCHAR(32)
)
RETURNS INT
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @RetVal INT
DECLARE @PostcodeVal INT
SET @RetVal = 0
SET @PostcodeVal = 0
SET @RetVal = @RetVal + dbo.FuzzyLogicStringMatch(@CompanyName, @CompanyName2)
IF @RetVal = 1 AND dbo.TelephoneNoStringMatch(@TelephoneNumber, @TelephoneNumber2) = 1
RETURN 5
ELSE
IF @RetVal = 1 AND dbo.FuzzyLogicStringMatch(@Postcode, @Postcode2) = 1
RETURN 5
ELSE
IF @RetVal = 1 AND ROUND(@Latitude,4) = ROUND(@Latitude2,4) AND ROUND(@Longitude,4) = ROUND(@Longitude2,4)
RETURN 5
ELSE
IF (@RetVal = 1 AND ROUND(@Latitude,4) = ROUND(@Latitude2,4)) OR (@RetVal = 1 AND ROUND(@Longitude,4) = ROUND(@Longitude2,4))
SET @RetVal = 2
ELSE
BEGIN
IF ROUND(@Latitude,4) = ROUND(@Latitude2,4)
SET @RetVal = @RetVal + 1
IF ROUND(@Longitude,4) = ROUND(@Longitude2,4)
SET @RetVal = @RetVal + 1
SET @RetVal = @RetVal + dbo.TelephoneNoStringMatch(@TelephoneNumber, @TelephoneNumber2)
SET @RetVal = @RetVal + dbo.FuzzyLogicStringMatch(@Postcode, @Postcode2)
END
RETURN @RetVal
END
This is the previous code that I am trying to fix:
SELECT li.LImportId, l.LocationId, dbo.MatchRanking(li.Latitude, li.Longitude, li.[Name], li.Postcode, li.TelephoneNumber,
l.Latitude, l.Longitude, l.CompanyName, l.Postcode, l.TelephoneNumber
) AS [MatchRanking]
FROM @LocImport li
LEFT JOIN [Location] l
ON lI.[Latitude] = l.[Latitude]
OR lI.[Longitude] = l.[Longitude]
OR lI.[Postcode] = l.[Postcode]
OR lI.[Name] = l.[CompanyName]
OR lI.[TelephoneNumber] = l.[TelephoneNumber]