Returning table with CLR

后端 未结 4 2057
名媛妹妹
名媛妹妹 2021-02-01 07:05

I want to write an CLR procedure which takes a text and returns a table with all the words in this text. But I can\'t figure out how to return a table. Could you please tell me

4条回答
  •  终归单人心
    2021-02-01 07:32

        [SqlFunction(DataAccess = DataAccessKind.Read, FillRowMethodName = "FillMatches", TableDefinition = "GroupNumber int, MatchText nvarchar(4000)")]
    public static IEnumerable Findall(string Pattern, string Input)
    {
        List GroupCollection = new List();
    
        Regex regex = new Regex(Pattern);
        if (regex.Match(Input).Success)
        {
            int i = 0;
            foreach (Match match in regex.Matches(Input))
            {
                GroupCollection.Add(new RegexMatch(i, match.Groups[0].Value));
                i++;
            }
        }
        return GroupCollection;
    }
    

    That was a slight alteration from the code by "Damon Drake"
    This one does a findall instead of returning the first value found. so

    declare @txt varchar(100) = 'Race Stat 2017-2018 -(FINAL)';
    select * from dbo.findall('(\d+)', @txt)
    

    returns

提交回复
热议问题