开发者

How to translate this regex in to a like clause

I have a stored procedure where one of the steps is to remove items that match a pattern

--Remove the databases to exclude
DELETE t
FROM #WhichDatabase t 
WHERE t.dbName like @dbExclude

However the pattern I want to use would be (in Regex)

^(chgTRK_.*)|(.*_Play)$
开发者_StackOverflow中文版

How do I achieve the same goal of passing in one string and parsing out a complex filter? I know I can do like 'chgTRK[_]%' or like '%[_]Play' but I am restricted because this is a stored procedure and there will not always be two parts to the filter.


Use a table valued parameter (SQL Server 2008) or CSV/XML/Temp table (SQL Server 2005 and earlier) to pass in your search term(s).

See "Arrays and Lists in SQL Server 2005 and Beyond" for more on this (the definitive article on this)

Assuming you have now loaded @DeleteConditions (table variable) with a column dbExclude

DELETE t
FROM
    #WhichDatabase t 
    JOIN
    @DeleteConditions d ON t.dbName like dbExclude


Dani's comment gave me a idea, one solution could be is make a CLR user-defined function to make sql support Regex.

A tutorial can be found here on how to make it.


Since you are writing a stored procedure, you can loop over the string you use as input (though a table input would be better) and do a delete for each pattern.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜