は、ここでは、そのような長さ、など、あなたの文字列のいくつかの簡単な属性、スペースの数などをサンプリングでき、あなたが試みることができる別の方法です;可能な文字列の組み合わせをすべて作成するためにクロス結合を使用できます。
次にwhere句の中で一致するものを並べ替えることができます。この例では、patindex()関数を使用して最初の文字列のサンプリングされた部分が2番目の文字列に含まれているかどうかを確認します。
-- begin sample table variable set up
declare @s table(
id int identity(1,1)
,string varchar(255)
,numSpace int
,numWord int
,lenString int
,firstPatt varchar(255)
);
declare @t table(
id int identity(1,1)
,string varchar(255)
,numSpace int
,numWord int
,lenString int
);
insert into @t(string)
values ('my name');
insert into @t(string)
values ('your name');
insert into @t(string)
values ('run and jump');
insert into @t(string)
values ('hello my name is');
insert into @s(string)
values ('name my');
insert into @s(string)
values ('name your');
insert into @s(string)
values ('jump and run');
insert into @s(string)
values ('my name is hello');
update @s
set numSpace = len(string)-len(replace(string,' ',''));
update @s
set numWord = len(string)-len(replace(string,' ',''))+1;
update @s
set lenString = len(string);
update @s
set firstPatt = rtrim(substring(string,1,charindex(' ',string,0)));
update @t
set numSpace = len(string)-len(replace(string,' ',''));
update @t
set numWord = len(string)-len(replace(string,' ',''))+1;
update @t
set lenString = len(string);
-- end sample table variable set up
-- select all combinations of strings using a cross join
-- and sort the entries in your where clause
-- the pattern index checks to see if the sampled string
-- from the first table variable is in the second table variable
select *
from
@s s cross join @t t
where
s.numSpace = t.numspace
and s.numWord = t.numWord
and s.lenString = t.lenString
and patindex('%'+s.firstPatt+'%',t.string)>0;
私は最初のテーブルを一度に1レコードずつループし、各レコードに対して一時テーブル変数を作成する必要がありますか?もしそうなら、それは私が避けたいものです。 – Colin
スプリット機能を含むように私の答えを編集しました。 – RKeast
ありがとう私は同様の分割機能を持っていますが、私は一度に何百ものレコードを実行する単一のバッチプロセスでこの機能をどのように組み込むことができるのかまだ分かりません。たぶん私は正しく説明していないか、あなたの答えを完全に理解していません。私はもう少しそれを使って遊びます。 – Colin