小弟目前碰到个CASE 是需要匹配数据表正则运算 regex
在这里也查看了CSDN上有讨论 ms sql上有用regex的范例
http://blog.csdn.net/jinjazz/article/details/4101746
但我还是遇上了麻烦
我的CLR 程序:
- SQL code
Imports SystemImports System.DataImports System.Data.SqlClientImports System.Data.SqlTypesImports Microsoft.SqlServer.ServerImports System.Text.RegularExpressionsPartial Public Class StoredProcedures <SqlFunction()> _ Public Shared Function regexes(ByVal expression As SqlString) As SqlString Dim regex As Regex = New Regex(" ") ' Split on hyphens. 'Dim substrings() As String = regex.Split("plum * pear * BALL") Dim substrings() As String = regex.Split(expression) Dim c As String = vbNullString For Each match As String In substrings If match = "*" Then '匹配有通配符才转换 match = ".*" End If c += match & " " Next Dim c1 As String = c.Trim(" ") 'Trim截去前后不必要的 Dim regex1 As String = c1 Return regex1 '返回值相当于 regexes = pattern End Function <SqlFunction()> _ Public Shared Function regexismatch(ByVal input As SqlString, ByVal pattern As SqlString) As SqlBoolean If input.IsNull OrElse pattern.IsNull Then Return False End If Return Regex.IsMatch(input.Value, pattern.Value) End Function <SqlFunction()> _ Public Shared Function regexmatch(ByVal input As SqlString, ByVal pattern As SqlString) As SqlString If input.IsNull OrElse pattern.IsNull Then End If Dim inputx As String = input Dim patternx As String = pattern Dim m As Match = Regex.Match(inputx, patternx) If m.Success Then Return input Else Return False End If End FunctionEnd Class
我写了三个函式 目前只用到两个 regexes, regexmatch
目前我的case是这样
要将这张数据表下的patter栏的字符串转成 regex 和 输入做匹配
输入正确就会回传输入字符串 这里用regexmatch 然后找出相对应的 template
目前小弟只知道将patter栏的字符串转成 regex这项工作
但和转成 regex的集和做匹配的sql 语法 小弟不知道该怎么写
这是我目前写的sql 碰到的错误信息
讯息 137,层级 16,状态 1,行 2
必须宣告纯量变量 "@t"。
还请各位大大指点一下迷津
------解决方案--------------------
- SQL code
--我本地没有vs环境,我用两个函数先代替一下,就是个思路问题。/*create function dbo.regexes (@p varchar(4))returns varchar(10)asbeginreturn @p+'a'endgocreate function dbo.regexmatch(@a varchar(10),@b varchar(10))returns varchar(10)asbeginreturn @[email protected]end*/--上面两个函数就是两个函数,没有实际意义,就是为了我本地能够正常运行。--假设这是你的aiml2的数据declare @aiml2 table (pattern varchar(2))insert into @aiml2select 'a1' union allselect 'b1' union allselect 'c1' union allselect 'd1'--你现在的实现方式declare @regex varchar(max)select @regex = dbo.regexes(pattern) from @aiml2select @regexdeclare @input varchar(max) set @input='ok ONES'select dbo.regexmatch(@input,@regex)--你现在的结果,只处理了一行/*ok ONESd1a*/--应该是这样的,[email protected]declare @input1 varchar(max) set @input1='ok ONES'select dbo.regexmatch(@input1,dbo.regexes(pattern)) from @aiml2/*ok ONESa1aok ONESb1aok ONESc1aok ONESd1a*/