Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning table with CLR

Tags:

c#

sql-server

clr

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 it?

    [Microsoft.SqlServer.Server.SqlFunction]
    public static WhatTypeShouldIWriteHere Function1(SqlString str)
    {
        string[] words = Regex.Split(str, @"\W+").Distinct().ToArray();
        //how to return a table with one column of words?
    }

Thank you for your help.

UPDATED: I need to do it for sql-2005

like image 335
StuffHappens Avatar asked Feb 11 '10 10:02

StuffHappens


People also ask

How do you implement CLR table-valued function?

Implementing Table-Valued Functions. Implement table-valued functions as methods on a class in a Microsoft . NET Framework assembly. Your table-valued function code must implement the IEnumerable interface.

What is CLR in SQL Server?

CLR Integration Security NET Framework common language runtime (CLR) manages and secures access between different types of CLR and non-CLR objects running within SQL Server. These objects may be called by a Transact-SQL statement or another CLR object running in the server.

Can we call SQL function from C#?

We can execute a function in C# using a SqlCommand object and passing a SQL defined function in a SELECT SQL query.

What are table-valued functions in SQL Server?

A table-valued function returns a single rowset (unlike stored procedures, which can return multiple result shapes). Because the return type of a table-valued function is Table , you can use a table-valued function anywhere in SQL that you can use a table.


1 Answers

Here is a full blown sample. I got tired of searching for this myself and even though this is answered, I thought I would post this just to keep a fresh reference online.

using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;

public partial class UserDefinedFunctions {    
  [SqlFunction]
  public static SqlBoolean RegexPatternMatch(string Input, string Pattern) {    
    return Regex.Match(Input, Pattern).Success ? new SqlBoolean(true) : new SqlBoolean(false);
  }

  [SqlFunction]
  public static SqlString RegexGroupValue(string Input, string Pattern, int GroupNumber) {

    Match m = Regex.Match(Input, Pattern);
    SqlString value = m.Success ? m.Groups[GroupNumber].Value : null;

    return value;
  }

  [SqlFunction(DataAccess = DataAccessKind.Read, FillRowMethodName = "FillMatches", TableDefinition = "GroupNumber int, MatchText nvarchar(4000)")]
  public static IEnumerable RegexGroupValues(string Input, string Pattern) {
    List<RegexMatch> GroupCollection = new List<RegexMatch>();

    Match m = Regex.Match(Input, Pattern);
    if (m.Success) {
      for (int i = 0; i < m.Groups.Count; i++) {
        GroupCollection.Add(new RegexMatch(i, m.Groups[i].Value));
      }
    }

    return GroupCollection;
  }

  public static void FillMatches(object Group, out SqlInt32 GroupNumber, out SqlString MatchText) {
    RegexMatch rm = (RegexMatch)Group;
    GroupNumber = rm.GroupNumber;
    MatchText = rm.MatchText;
  }

  private class RegexMatch {
    public SqlInt32 GroupNumber { get; set; }
    public SqlString MatchText { get; set; }

    public RegexMatch(SqlInt32 group, SqlString match) {
      this.GroupNumber = group;
      this.MatchText = match;
    }
  }
};
like image 171
Damon Drake Avatar answered Oct 16 '22 02:10

Damon Drake