Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Issue with slow find and replace function code from a lookup table can I do this in a better way

$
0
0
I have some dim tables for find and replace for colours and sizes as I have multiple columns in a table that could hold the value see below,FilterColour = CASE WHEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.colour + ' ','/',' ')) lc) IS NOT NULL THEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.colour + ' ','/',' ')) lc) WHEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.model_number + ' ','/',' ')) lc) IS NOT NULL THEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.model_number + ' ','/',' ')) lc) WHEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.commission_group + ' ','/',' ')) lc) IS NOT NULL THEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.commission_group + ' ','/',' ')) lc) WHEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.custom_1 + ' ','/',' ')) lc) IS NOT NULL THEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.custom_1 + ' ','/',' ')) lc) WHEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.custom_2 + ' ','/',' ')) lc) IS NOT NULL THEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.custom_2 + ' ','/',' ')) lc) WHEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.custom_3 + ' ','/',' ')) lc) IS NOT NULL THEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.custom_3 + ' ','/',' ')) lc) WHEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.custom_4 + ' ','/',' ')) lc) IS NOT NULL THEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.custom_4 + ' ','/',' ')) lc) WHEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.custom_5 + ' ','/',' ')) lc) IS NOT NULL THEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.custom_5 + ' ','/',' ')) lc) WHEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.product_name + ' ','/',' ')) lc) IS NOT NULL THEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.product_name + ' ','/',' ')) lc) WHEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.[description] + ' ','/',' ')) lc) IS NOT NULL THEN (SELECT lc.ReturnString FROM dbo.UTfn_LikeColourTableFilter(REPLACE(SFA.[description] + ' ','/',' ')) lc) ENDThe function cod is hereALTER FUNCTION [dbo].[UTfn_LikeColourTableFilter] ( @Value VARCHAR(8000))RETURNS TABLE AS RETURN WITH ColourList AS ( -- replace this section with your Colour table when the time comes. SELECT SCT.LookupValue,SCT.DisplayInFilter FROM dbo.Dim_Colour SCT ), LookupList AS ( SELECT ROW_NUMBER() OVER (ORDER BY LEN(cl.LookupValue) DESC) AS rn, -- prevents "Yellow" from butchering up "LightGoldenRodYellow".. cl.LookupValue,cl.DisplayInFilter FROM dbo.Dim_Colour cl WHERE @Value LIKE '%' + cl.LookupValue + '%' ), RecursiveReplace AS ( -- -- My select 1 SELECT cis.DisplayInFilter AS ReturnString, cis.rn FROM LookupList cis WHERE cis.rn = 1 UNION ALL -- -- My select 2 SELECT cis.DisplayInFilter AS ReturnString, cis.rn FROM LookupList cis JOIN RecursiveReplace rr ON cis.rn = rr.rn + 1 ) SELECT TOP 1 rr.ReturnString FROM RecursiveReplace rr ORDER BY rr.rn DESC;The dbo.Dim_Colour table has 519 rows I use the same processes for size but that dim table has over 2500 rows and runs even slowerI did think about adding another parameter for size type but couldn't get it to work :(The function was the one I used from a post here about a year ago but it's just to slow :(Many thanks for any help with this issue :)

Viewing all articles
Browse latest Browse all 3145

Trending Articles