I am trying to create a simple CLR function with a table evaluation that takes a comma-separated string as a parameter, splits it, and returns it as multiple lines
After a few online tutorials, I ended up:
[SqlFunction(FillRowMethodName = "FillRow",TableDefinition="val nvarchar(1000)")]
public static IEnumerable SqlFunction1(SqlString val)
{
string[] splitStr = val.Value.Split(',');
return splitStr;
}
private static void FillRow(Object obj, out SqlString str)
{
object[] row = (object[])obj;
str = (string)row[0];
}
However, performing it with
select * from dbo.SqlFunction1('1,2,3,4,5')
Returns the following error
Msg 6260, Level 16, State 1, Line 1
An error occurred while getting new row from user defined Table Valued Function :
System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Object[]'.
System.InvalidCastException:
at UserDefinedFunctions.FillRow(Object obj, SqlString& str)
.
Akash source
share