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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:51:43+00:00 2026-05-20T11:51:43+00:00

Supossing we have n columns with m rows table 1: someName1 someName2 someName3 …

  • 0

Supossing we have n columns with m rows

table 1:

someName1  someName2  someName3 ... someNameN
----------------------------------------------
12.5       12.34      56.6     ...    33.2
1.2323     12.5       57.2     ...    123.1
2.789      45.2       766.1    ...    56.2  
45.23      34.3       7.4      ...    33.4
52.1       4.3        89.8     ...    67.3

How to use dynamic SQL to do in general

Output (A table with n rows, with,autoincrement ID, column name of Table1 and Sum of column like):

ID    Column     Result
--------------------------------
1     someName1  SUM(someName1)=12.5+1.2323+2.789+45.23+52.1
2     someName2  SUM(someName2)=12.34+12.5+45.2+34.3+4.3
3     someName3  SUM(someName3)=56.6+57.2+766.1+7.4+89.8
...   ...
...   ...
N     someNameN  SUM(someName3)=33.2+123.1+56.2+33.4+67.3

Where SUM(columnName) is the value of summing all values of Table 1, How to do this for any size of any table, where n could be 50, in other words a table with 50 columns??

  • 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-20T11:51:44+00:00Added an answer on May 20, 2026 at 11:51 am

    It might look like this:

    declare @tableName nvarchar(128) = N'table_with_50_columns'
    declare @columnLikeFilter  nvarchar(128) = N'someName%'
    
    declare @columns nvarchar(2000) = '';
    declare @sumColumns nvarchar(2000) = '';
    
    select @columns = @columns + COLUMN_NAME + ',',
           @sumColumns = @sumColumns +  'sum(' + COLUMN_NAME + ') as ' + COLUMN_NAME + ','
    from INFORMATION_SCHEMA.COLUMNS 
    where TABLE_NAME = @tableName and COLUMN_NAME like @columnLikeFilter
    order by ORDINAL_POSITION ;
    
    set @columns = left(@columns, len(@columns) - 1) ;
    set @sumColumns = left(@sumColumns, len(@sumColumns) - 1) ;
    
    declare @sql nvarchar(4000) =
        N';with cteColumnts (ORDINAL_POSITION, COLUMN_NAME) as 
        (
            select ORDINAL_POSITION, COLUMN_NAME 
            from INFORMATION_SCHEMA.COLUMNS 
            where TABLE_NAME = N'''+ @tableName + ''' and COLUMN_NAME like ''' + @columnLikeFilter + '''
        ),
        cteValues (ColumnName, SumValue) as
        (
            SELECT ColumnName, SumValue
            FROM 
               (SELECT ' + @sumColumns + '
               FROM dbo.' + @tableName + ') p
            UNPIVOT
               (SumValue FOR ColumnName IN 
                  (' + @columns + ')
            )AS unpvt 
        )
        select row_number() over(order by ORDINAL_POSITION) as ID, ColumnName, SumValue
        from cteColumnts c inner join cteValues v on COLUMN_NAME = ColumnName
        order by ORDINAL_POSITION'
    
    --print @sql
    
    exec sp_executesql @sql
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I have an oracle database table with a lot of columns that I'm
Supposing my table has 10 columns .... which columns i have to choose to
There are two tables: table A and table B. They have the same columns
In a previous question @Morawski was saying that a table with 1,000 columns and
Just similar other post, I need to retrieve any rows from table applying criteria
I need to store select statement results (multiple rows and Columns) in a variable
i have a table with 2 colums, id and date. supposing i have some
supposing that I have two tables with same 3 columns: ID (int, identity), text
Supposing you have: <div><p>Some text</p><p>Some text</p></div> <div><p id='paragraph'>Some text</p></div> How can you use javascript
Supposing we have the following records in an SQL Server table. Date 19/5/2009 12:00:00

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.