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 6334403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:39:08+00:00 2026-05-24T18:39:08+00:00

I want to create CLR UDF to scan a SQL table and read each

  • 0

I want to create CLR UDF to scan a SQL table and read each column and sum all data for
each column (I am working with big datasets, like >1000 columns and 20,000,000>rows).

I’d like to compare applying for each column SUM(COLUMN_NAME) SQL function with parallelized for loop

So The SQL would look like

SELECT SUM(COLUMN_1),SUM(COLUMN_2),SUM(COLUMN_3),...,SUM(COLUMN_1000)

How can I do a CLR UDF in C# that would do that?

I am planning to use an array, so each time I read the row I add it like:
array[i]+= sqlValue;

How to do this, so I can execute both of them in a stored proc?

Table

column_1  column_2 column_3   .... column_1000
---------------------------------------------
     451      57     253                 135
     251      77     356                 965
     481      15     323                 655  
     452      15     135                 665
    ... 
    ...20,000,000 more rows
  • 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-24T18:39:08+00:00Added an answer on May 24, 2026 at 6:39 pm

    So what you’re trying to achieve is to do a thousand SELECT column_x FROM table, one for each column, and do the summing from hand.

    That means 1000 parallel connections to the database all working within the same table within the same rows, locking each other (until you use with nolock).

    The benefit to SELECT sum(column_1), sum(column_2), .... I can’t see one and I believe what you’re trying will be magnitudes slower than letting SQL Server doing what it does best.

    EDIT:

    As per your request here’s a quick’n’dirty sample, not tested since I currently have no SQL server at hand. I assumed the columns are of type long and the result is of type decimal.

    public partial class UserDefinedFunctions
    {
        private static string GetCommandText(int column)
        {
            return string.Format("select column_{0} from table", column);
        }
    
        [Microsoft.SqlServer.Server.SqlFunction(
            DataAccess = DataAccessKind.Read,
            TableDefinition = "result decimal",
            FillRowMethodName = "FillRow",
            SystemDataAccess = SystemDataAccessKind.Read)]
        public static IEnumerable fnSum(int columnNo)
        {
            var values = new List<long>();
    
            using (var cmd = new SqlCommand(GetCommandText(columnNo), new SqlConnection("context connection=true")))
            {
                cmd.Connection.Open();
                using (var reader = cmd.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        values.Add(reader.GetInt64(0));
                    }
                }
            }
    
            return list;
        }
    
        private static void FillRow(object obj, out decimal result)
        {
            var values = (List<long>)obj;
    
            result = values.Sum(value => (decimal) value);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use CLR table-valued function in SQL Server 2008, which accesses a
i want to create a CLR function, i created a normal class library file
I want to create a Generic DataGrid to use on all my Views/UserControls. This
I want create a drop shadow around the canvas component in flex. Technically speaking
I want create a excel with Apache POI in java and I must insert
i want create image animation , i have 50 images with png format now
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic

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.