我第二評論即TSQL只是不適合這種操作,但如果你必須在DB做在這裏是一個用函數來管理多個替代語句的例子。
既然你在每一個音符(5-15)和一個非常大的數字標記(1萬-10萬),我的功能首先從輸入作爲潛在令牌提取的令牌,並使用該組的數量相對較少的令牌加入你的查詢(下面的dbo.Token)。在每個音符中查找您的代幣的的任何都是非常麻煩的。
我做了一些使用50k標記和5k筆記的perf測試,這個功能運行得非常好,在<(在我的筆記本電腦上)完成了<。請回報此策略如何爲您執行。
注:在您的示例數據令牌格式不統一(##_#, ##_##, #_#
),我猜測這只是一個錯字,並承擔所有標記取## TokenName的形式##。
--setup
if object_id('dbo.[Lookup]') is not null
drop table dbo.[Lookup];
go
if object_id('dbo.fn_ReplaceLookups') is not null
drop function dbo.fn_ReplaceLookups;
go
create table dbo.[Lookup] (LookupName varchar(100) primary key, LookupValue varchar(100));
insert into dbo.[Lookup]
select '##placeholder130##','Dog' union all
select '##myPlaceholder##','Cat' union all
select '##oneMore##','Cow' union all
select '##test##','Horse';
go
create function [dbo].[fn_ReplaceLookups](@input varchar(max))
returns varchar(max)
as
begin
declare @xml xml;
select @xml = cast(('<r><i>'+replace(@input,'##' ,'</i><i>')+'</i></r>') as xml);
--extract the potential tokens
declare @LookupsInString table (LookupName varchar(100) primary key);
insert into @LookupsInString
select distinct '##'+v+'##'
from ( select [v] = r.n.value('(./text())[1]', 'varchar(100)'),
[r] = row_number() over (order by n)
from @xml.nodes('r/i') r(n)
)d(v,r)
where r%2=0;
--tokenize the input
select @input = replace(@input, l.LookupName, l.LookupValue)
from dbo.[Lookup] l
join @LookupsInString lis on
l.LookupName = lis.LookupName;
return @input;
end
go
return
--usage
declare @Notes table ([Id] int primary key, notes varchar(100));
insert into @Notes
select 1, 'This is some notes ##placeholder130## this ##myPlaceholder##, ##oneMore##. End.' union all
select 2, 'Second row...just a ##test##.';
select *,
dbo.fn_ReplaceLookups(notes)
from @Notes;
返回:
Tokenized
--------------------------------------------------------
This is some notes Dog this Cat, Cow. End.
Second row...just a Horse.
你可以發佈需要10-30秒的當前解決方案嗎? –
你有沒有考慮過使用SQL CLR? – RBarryYoung
@RBarryYoung,是的,我的服務器啓用了CLR,但我無法將表傳遞給CLR,AFAIK CLR不允許傳遞數據表。 – user194076