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

  • Home
  • SEARCH
  • 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 7429209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:55:58+00:00 2026-05-29T08:55:58+00:00

I have a t-sql user-defined-sp (call it sp1 ) which gets userId as param

  • 0

I have a t-sql user-defined-sp (call it sp1) which gets userId as param and return

I want to create a new user-defined-sp (call it sp2) that gets a list of userIds.

sp2 executes sp1 for each userId and union all output to one big output.

Each set of outputs for userId=a will be consecutive in the big table

i.e. the big table can look like:

a  result1,1 
a  result1,2
a  result1,3
b  result2,1
b  result2,1

how can I do this?

  • 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-29T08:55:59+00:00Added an answer on May 29, 2026 at 8:55 am

    Inside the main stored proc (sp2), you can create a table variable, and populate it with the data, then select all from that table variable. Something like this:

    DECLARE @loop_tbl TABLE (ID int identity(1,1), UserID int)
    DECLARE @return_tbl TABLE (column_a varchar(1), column_result varchar(20), column_int int)
    DECLARE @min int, @max int
    
    INSERT INTO @loop_tbl(UserID) SELECT UserID FROM Users
    SELECT @min=MIN(ID), @max=MAX(ID) FROM @loop_tbl
    
    WHILE @min <= @max
    BEGIN
        INSERT INTO @return_tbl (column_a, column_result, column_int)
        EXEC sp1 @userid=(SELECT UserID FROM @loop_tbl WHERE ID=@min)
        --Proc must return a data set using SELECT with the proper columns in the proper order
    
        SET @min=@min+1
    END
    
    SELECT * FROM @return_tbl 
    

    The only thing to watch out for is that INSERT EXEC will fail if sp2 is nested inside of another stored proc.

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

Sidebar

Related Questions

I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName]
I have a User defined function in SQL Server 2008. Here it is: CREATE
I have a user defined function in a SQL Server 2005 database which returns
I have a user defined function in SQL called getBuisnessDays it takes @startdate and
I have an user defined table function in SQL Server that aggregate data from
I have a multi-user ASP.NET app running against SQL Server and want to have
I have created one user named tuser with create database rights in SQL server
I have a user defined type, which is a datatable in an SqlServer database.
Using SQL 2008 So I created a User Defined type: CREATE TYPE dbo.ServiceType AS
I have an ASP.NET MVC view which contains checkboxes for user-defined categories. <td><% foreach

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.