Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 661637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:17:21+00:00 2026-05-13T23:17:21+00:00

How can I return a identity column with a standard SQL CLR Split UDF

  • 0

How can I return a identity column with a standard SQL CLR Split UDF for example the code below will return a table with the string value split by delimiter, I need to somehow return an identity column as well.

<SqlFunction(FillRowMethodName:="FillRow", TableDefinition:="value nvarchar(4000)")> _
Public Shared Function GetStrings(ByVal str As SqlString, ByVal delimiter As SqlString) As IEnumerable
    If (str.IsNull OrElse delimiter.IsNull) Then
        Return Nothing
    Else
        Return str.Value.Split(CChar(delimiter))
    End If
End Function
Public Shared Sub FillRow(ByVal row As Object, ByRef str As String)
    str = CType(row, String).Trim()
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:17:21+00:00Added an answer on May 13, 2026 at 11:17 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer So... in discussing MVC, you should understand that the controller… May 14, 2026 at 10:26 pm
  • Editorial Team
    Editorial Team added an answer Indeed, generating the whole site at each build is clearly… May 14, 2026 at 10:26 pm
  • Editorial Team
    Editorial Team added an answer Something like AutoIt? http://www.autoitscript.com/autoit3/index.shtml May 14, 2026 at 10:26 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.