這是我在下面嘗試使用兩個引數和一個作為匹配詞的輸出的函式。我使用@searchentry 和@bestmatch 作為我的引數。我的問題是引數應該放在函式中的哪個位置,以便我可以在創建函式時呼叫函式,它將Select dbo.FunMatch('enamel cleaner', 'cleaner')執行函式并從兩個引數回傳匹配的單詞 1 ?
Create Function dbo.FunMatch(
@searchentry varchar,
@bestmatch varchar
)
Returns INT
As
Begin
Declare @output INT
Set @output = (select
@searchentry,
@bestmatch,
cast(count(isMatch) as float) as matchingWords
from
(
select
s.value as word_from_search_entry_txt,
b.value as word_from_best_match,
case
when s.value = b.value or s.value 's'=b.value or s.value=b.value 's' then 'match'
else null
end as isMatch,
t.*
from (
SELECT
@searchentry,@bestmatch
FROM #tmp_parts
) t
cross apply
string_split(@searchentry, ' ') s
cross apply
string_split(@bestmatch, ' ') b
) a
group by
@searchentry,
@bestmatch)
Return @output
我正在撰寫一個函式來回傳兩個字串之間的匹配詞。下面的示例資料
CREATE TABLE #tmp_parts
(
search_entry_txt VARCHAR(30),
best_match VARCHAR(30),
);
INSERT INTO #tmp_parts
VALUES ('rotating conveyors', 'conveyor'),
('rivet tool', 'rivet nut tool'),
('enamel cleaner', 'cleaner'),
('farm ring', 'ring'),
('tire gauge', 'gauge'),
('ice cream','ice cream');
您可以在這里看到預期的匹配詞列
select
search_entry_txt,
best_match,
cast(count(isMatch) as float) as matchingWords
from
(
select
s.value as word_from_search_entry_txt,
b.value as word_from_best_match,
case
when s.value = b.value or s.value 's'=b.value or s.value=b.value 's' then 'match'
else null
end as isMatch,
t.*
from (
SELECT
search_entry_txt,best_match
FROM #tmp_parts
) t
cross apply
string_split(search_entry_txt, ' ') s
cross apply
string_split(best_match, ' ') b
) a
group by
search_entry_txt,
best_match
uj5u.com熱心網友回復:
您的函式腳本存在一些問題。
- 引數
@searchentry,@bestmatch可能會添加型別長度,否則會將長度宣告為 1。 - 您缺少
END功能端的 。 - 從您的代碼中,您不需要使用
#tmp_parts臨時表,只需使用 parameters@searchentry,@bestmatch. - 您可能不需要一些冗長的腳本,(
group by部分,子查詢可以使用聚合函式來代替)
我已經重寫了你的腳本,你可以試試這個。
Create Function dbo.FunMatch(
@searchentry varchar(max),
@bestmatch varchar(max)
)
Returns INT
As
Begin
Declare @output INT
set @output =(select
COUNT(case
when s.value = b.value or s.value 's'=b.value or s.value=b.value 's' then 'match'
else null
end)
from
string_split(@searchentry, ' ') s
cross apply
string_split(@bestmatch, ' ') b)
Return @output
END
sqlfiddle
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/438314.html
上一篇:函式結果中的排名不匹配
