文字列の一致基準に基づいて一連のテーブルを結合し、結合されたテーブルを検索して、情報がどのように一致したかの説明と共に情報を返すように設計された、以前の開発者ストアド プロシージャを修正しています。
このプロシージャーは現在、結合されたテーブルの各列を調べ、一致するものが見つかると、結果テーブルに新しい行を作成し、ソース テーブルの次の列に進みます。問題は、複数の一致が連続して存在する場合で、最初の一致以降の結果が得られないことです。
次のようなテーブルが必要です。
----A---B---C---D---E---F---G---H---I---
| |
1.-------------------------------------|
| N Y Y N N N N N N |
2.--------------------------------------
| Y N N N N N N N N |
3.--------------------------------------
| N Y N N Y Y N N N |
----------------------------------------
テーブルを作成するには:
----A---B---C---D---E---F---G---H---I---
| |
1.--------------------------------------
| found a match in row 1-B |
2.--------------------------------------
| found a match in row 1-C |
3.--------------------------------------
| found a match in row 2-A |
4.--------------------------------------
| found a match in row 3-B |
5.--------------------------------------
| found a match in row 3-E |
6.--------------------------------------
| found a match in row 3-F |
----------------------------------------
チェックする必要がある列ごとに個別のルーチンを作成するよりも、すべての一致を見つける効率的な方法はありますか。カーソルで問題を解決できると言われましたが、このクエリの実行にはかなりの時間がかかります。
ここで役立つ場合は、元の手順です。
SELECT
CASE WHEN (rpfname LIKE @SearchString OR rplname LIKE @SearchString) AND rlrelcode = 'A' THEN 2
WHEN ((rpfname LIKE @SearchString OR rplname LIKE @SearchString) AND (rlrelcode LIKE 'c%' OR rlrelcode LIKE 'REL%' OR rlrelcode = 'E')) OR clname1 LIKE @SearchString OR clname2 LIKE @SearchString OR clcontact LIKE @SearchString OR clrefer LIKE @SearchString OR mcontact LIKE @SearchString THEN 3
ELSE 1 END AS type,
ISNULL(CAST(mmatter AS VARCHAR(100)), '<Matter does not exist>') AS client_matter_number,
clnum,
mdesc1 AS matter_description,
ISNULL(mbillaty, udvalue) AS ttk,
ISNULL(mopendt, clopendt) AS open_date,
mclosedt AS close_date,
LTRIM(ISNULL(clname1, '') + ISNULL(' ' + clname2, '')) AS client_name,
CASE WHEN (rpfname LIKE @SearchString OR rplname LIKE @SearchString ) THEN LTRIM(ISNULL(rpfname, '') + ISNULL(' ' + rplname, ''))
WHEN rddesc LIKE @SearchString THEN LTRIM(rddesc )
WHEN (clname1 LIKE @SearchString OR clname2 LIKE @SearchString ) THEN LTRIM(ISNULL(clname1, '') + ISNULL(' ' + clname2, ''))
WHEN clcontact LIKE @SearchString THEN LTRIM(clcontact)
WHEN clrefer LIKE @SearchString THEN LTRIM(clrefer)
WHEN cddesc LIKE @SearchString THEN LTRIM(cddesc)
WHEN mname LIKE @SearchString THEN LTRIM(mname)
WHEN mdesc1 LIKE @SearchString THEN LTRIM(mdesc1)
WHEN mddesc LIKE @SearchString THEN LTRIM(mddesc)
WHEN mcontact LIKE @SearchString THEN LTRIM(mcontact)
WHEN fdesc1 LIKE @SearchString THEN LTRIM(fdesc1)
WHEN fdnarr LIKE @SearchString THEN LTRIM(fdnarr)
WHEN subdesc1 LIKE @SearchString THEN LTRIM(subdesc1)
WHEN subnarr LIKE @SearchString THEN LTRIM(subnarr) END AS found_search_string,
CASE WHEN rpfname LIKE @SearchString OR rplname LIKE @SearchString THEN dbo.stcGetRelationNarrative(rlindex)
ELSE '' END AS notes,
CASE WHEN (rpfname LIKE @SearchString OR rplname LIKE @SearchString) THEN rlrelation
ELSE '' END AS relationship,
CASE WHEN (rpfname LIKE @SearchString OR rplname LIKE @SearchString) AND rlrelcode = 'A' THEN 'A'
WHEN ((rpfname LIKE @SearchString OR rplname LIKE @SearchString) AND (rlrelcode LIKE 'c%' OR rlrelcode LIKE 'REL%' OR rlrelcode = 'E')) OR clname1 LIKE @SearchString OR clname2 LIKE @SearchString OR clcontact LIKE @SearchString OR clrefer LIKE @SearchString OR mcontact LIKE @SearchString THEN 'CF'
ELSE '?' END AS relation_code,
CASE WHEN (rpfname LIKE @SearchString OR rplname LIKE @SearchString ) THEN 'RP Name'
WHEN rddesc LIKE @SearchString THEN 'RP Narrative'
WHEN (clname1 LIKE @SearchString OR clname2 LIKE @SearchString) THEN 'Client Name'
WHEN clcontact LIKE @SearchString THEN 'Client Contact'
WHEN clrefer LIKE @SearchString THEN 'Client Referral'
WHEN cddesc LIKE @SearchString THEN 'Client Narrative'
WHEN mname LIKE @SearchString THEN 'Matter Name'
WHEN mdesc1 LIKE @SearchString THEN 'Matter Description'
WHEN mddesc LIKE @SearchString THEN 'Matter Narrative'
WHEN mcontact LIKE @SearchString THEN 'Matter Contact'
WHEN fdesc1 LIKE @SearchString THEN 'Folder Description'
WHEN fdnarr LIKE @SearchString THEN 'Folder Narrative'
WHEN subdesc1 LIKE @SearchString THEN 'Submatter Description'
WHEN subnarr LIKE @SearchString THEN 'Submatter Narrative' END AS source_of_information
FROM crlparty rp WITH (NOLOCK)
LEFT OUTER JOIN
crllink r WITH (NOLOCK)
ON r.rpindex = rp.rpindex
FULL OUTER JOIN
matter m WITH (NOLOCK)
ON rlmatter = m.mrelated
LEFT OUTER JOIN
mattdesc md WITH (NOLOCK)
ON m.mmatter = md.mmatter
FULL OUTER JOIN
client c WITH (NOLOCK)
ON ISNULL(m.mclient, r.rlclnum) = c.clnum
LEFT OUTER JOIN
clidesc cd WITH (NOLOCK)
ON c.clnum = cd.clnum
LEFT OUTER JOIN
crldesc rpd WITH (NOLOCK)
ON rpd.rpindex = r.rpindex
LEFT OUTER JOIN
udf u WITH (NOLOCK)
ON u.udjoin = c.clnum AND
u.udfindex=40
FULL OUTER JOIN
folder f WITH (NOLOCK)
ON m.mmatter = f.fmatter
LEFT OUTER JOIN
foldnarr fn WITH (NOLOCK)
ON f.findex = fn.findex
FULL OUTER JOIN
submatter s WITH (NOLOCK)
ON r.rlmatter = s.smatter
LEFT OUTER JOIN
subnarr sn WITH (NOLOCK)
ON s.sindex = sn.sindex
WHERE (rp.rpfname LIKE @SearchString OR rp.rplname LIKE @SearchString ) OR
rpd.rddesc LIKE @SearchString OR
(c.clname1 LIKE @SearchString OR c.clname2 LIKE @SearchString ) OR
c.clcontact LIKE @SearchString OR
c.clrefer LIKE @SearchString OR
cd.cddesc LIKE @SearchString OR
m.mname LIKE @SearchString OR
m.mdesc1 LIKE @SearchString OR
md.mddesc LIKE @SearchString OR
m.mcontact LIKE @SearchString OR
f.fdesc1 LIKE @SearchString OR
fn.fdnarr LIKE @SearchString OR
s.subdesc1 LIKE @SearchString OR
sn.subnarr LIKE @SearchString