Showing posts with label cosnistently. Show all posts
Showing posts with label cosnistently. Show all posts

Monday, March 19, 2012

Error Creating CLR UDF

Hi:

I am trying to create a CLR UDF in SQL 2005 and cosnistently run into the following error. What am I doing wrong?. Please correct me and show me the right way of doing this.

Msg 6551, Level 16, State 2, Procedure EmailSplitter, Line 3

CREATE FUNCTION for "EmailSplitter" failed because T-SQL and CLR types for return value do not match.

Here is what I am trying to achieve. Split a Email field in the database. For that I am trying to return an array using C# and then trying to call the UDF for the C#.

--1).CLR Code. (EmailSpitter.cs)

using System;
using System.Collections.Generic;
using System.Text;

namespace SQLTools
{
public class EmailSplitter
{
public static string[] Parse(string data)
{
string[] columnData;
string[] separators = new string[1];
separators[0] = " ";

if (data != null)
{
columnData = data.Split(separators, StringSplitOptions.None);
return columnData;
}
return null;
}
}
}

--2). Assembly code.

CREATE Assembly SQLArrayBuilder

FROM 'E:\CLR\EmailSplitter\bin\Debug\EmailSplitter.dll'

WITH PERMISSION_SET=SAFE

Select * from sys.assemblies.

--3). Create the function.

CREATE Function dbo.EmailSplitter

(@.EmailString NVARCHAR(4000))

RETURNS VARCHAR(4000)

AS

EXTERNAL NAME SQLArrayBuilder.[SQLTools.EmailSplitter].Parse

Run into the error:

Msg 6551, Level 16, State 2, Procedure EmailSplitter, Line 3

CREATE FUNCTION for "EmailSplitter" failed because T-SQL and CLR types for return value do not match.

Please help me.

Thank you very much.

AK

Your CREATE FUNCTION returns a string, but your CLR function returns an array of strings, that is why you are getting an error. If you want to return an array of strings you will have to create a CLR function that returns an IEnumerable object and define a table valued function in T-SQL. Look up CLR table valued functions in the BOL for the details.

Dan