In terms of a CLR UDF, "identity" doesn't have any particular meaning, since you are generating all of the rows yourself. All you want is a counter.
Simple answer would be to just generate the indexes on the spot and then send an IEnumerable
of composite values. So something like this:
[SqlFunction(FillRowMethodName = "FillMyRow",
TableDefinition = "ID int, Value nvarchar(4000)")]
public static IEnumerable GetStrings(SqlString str, SqlString delimiter)
{
if (str.IsNull || delimiter.IsNull)
{
return null;
}
string[] values = str.Value.Split(delimiter.Value.ToCharArray());
StringPair[] results = new StringPair[values.Length];
for (int i = 0; i < values.Length; i++)
{
results[i] = new StringPair(i + 1, values[i]);
}
return results;
}
public static void FillMyRow(object row, ref int id, ref string value)
{
StringPair pair = (StringPair)row;
id = pair.ID;
value = pair.Value;
}
public class StringPair
{
public StringPair(int id, string value)
{
this.id = id;
this.value = value;
}
public int ID { get; private set; }
public string Value { get; private set; }
}
It's the exact same thing as an identity column; you're just incrementing a counter for the ID, starting from the number 1.
You might also want to consider that a surrogate ID can be generated in SQL itself with ROW_NUMBER
, so it may not be necessary to do this at all. That would be my choice, personally, but if you need to do it in the CLR output, the above code should do the trick.