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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:25:59+00:00 2026-05-27T01:25:59+00:00

I have a question about inserting column stored procedure into table. I have two

  • 0

I have a question about inserting column stored procedure into table.
I have two tables about 100 rows and 1 Mil rows.
Basically, I have a function that returns int comparing two data from different table.
And I want to run function on each combination of data.

Table 1 (~100 rows)

Col1                
A                   
B                   
C                   
D  

Table 2 (~1 Mil rows)

Col1  
1  
2  
3  
4  

function(i,j) where i is from table 1 and j is from table 2
Code in nutshell is like:

while i < count(*) table 1  
   print i  
   while j < count(*) table 2  
   select function(i,J)  
   J = J + 1  
   end  
i = i + 1  
j = 0  
end  

The result display is a long single column with i (from table1) and int evaluated from function.

i(1)  
1  
3  
4  
.  
.  
.  
i(2)  
1  
2  
.  
.  

I want to display the above result like table dynamically assign column name to i.

i(1)        i(2)        i(3) .....  
1           1           5  
3           2           9  
4           3           4  

Thanks in advance.

  • 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-27T01:26:00+00:00Added an answer on May 27, 2026 at 1:26 am

    This would be my solution:

    DECLARE @columns VARCHAR(MAX)
    
    SELECT @columns = COALESCE(@columns + ',[' + CAST(t1.i as varchar(10)) + ']'
                               ,'[' + CAST(t1.i as varchar(10)) + ']')
    FROM t1
    
    DECLARE @query VARCHAR(MAX)
    SET @query = '
        SELECT * FROM
        (
            SELECT t1.i, t2.j, yourFunction(t1.i, t2.j) as functionResult
            FROM t1
            CROSS JOIN t2
        ) as tbl3
        PIVOT
        (
            SUM(functionResult) FOR i in (' + @columns + ')
        ) AS tblResults'
    
    EXECUTE (@query)​
    

    You can check out a working example of this here on SEDE.

    Edit

    And just for clarification, you would replace t1 and t2 with the actual names of your 2 tables. Also in the SEDE example I just added i and j together (t1.i + t2.j as functionResult) because I can’t create functions on there, but you would just call your function instead like I show in my answer (yourFunction(t1.i, t2.j) as functionResult).

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

Sidebar

Related Questions

I have a question about inserting values into an input field from my MYSQL
I have a relation between two tables with 600K rows and my first question
Previously I've asked about inserting a column into a dataset . I now have
I have a question about how to deploy WPF application into a PC without
I have a question about tables in MySQL. I'm currently making a website where
I have two databases($db1, $db2) with exact table structure(table1). $db2.table1 has new rows which
I have a question about inserting row order number by spesific order type. Products
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question about normalization. Suppose I have an applications dealing with songs. First
I have a question about using streams in .NET to load files from disk.

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.