我嘗試了以下方法。請檢查:
create table one(
keyid int,
keyword varchar(10)
)
insert into one values (1,'Small')
insert into one values (2,'Medium')
insert into one values (3,'Large')
insert into one values (4,'XXLarge')
create table two(
logid int,
description varchar(100))
insert into two values ('1','blah blah.. Medium blah')
insert into two values ('2','dsdms Large whatever')
alter table two add keyid int
這是查詢按照您的要求
declare @query varchar(800),@keyid int,@keyword varchar(100),@description varchar(max),@LOGID INT
declare d cursor for
select keyid,keyword from one
open d
fetch next from d into @keyid,@keyword
while @@FETCH_STATUS=0
begin
declare c cursor for
select DESCRIPTION,LOGID from two
open c
fetch next from c into @description, @logid
while @@FETCH_STATUS=0
BEGIN
set @query='if exists(select * from two where upper(description) like ''%' +upper(@keyword)+'%'' and logid='+cast(@LOGID as varchar(max))+')
begin
update two set keyid=(select keyid from one where upper(keyword)=''' +upper(@keyword)+''') where logid='+cast(@LOGID as varchar(max))+'
end'
print @query
exec (@query)
fetch next from c into @description, @logid
end
close c
deallocate c
fetch next from d into @keyid,@keyword
end
close d
deallocate d
select * from two
最快的方法是使用SQL Server的全文搜索功能來生成FTS索引。其他任何代碼都會很慢並且執行速度很慢 –
「最快」的含義是什麼?你的意思是你必須這樣做*一次*,並且想要快速和骯髒地完成這個過程而不要設置太多?或者你是否想要建立一個堅固的過程,可以一次又一次地實現新的傳入數據和穩步增長的表格? – Ralph
只是好奇,你如何做一個類似的描述:「等等等等等等等等等等等等等等等等等等。 – JFM