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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:56:36+00:00 2026-06-09T12:56:36+00:00

I have a Stored Procedure which right now returns a (potentially) large data set.

  • 0

I have a Stored Procedure which right now returns a (potentially) large data set. This data is from a recursive call so it looks something like this:

With Hierarchy as(

SELECT distID as D, [Name] as N, enrolling as E, parent_code as P, 
        FROM sales_reps
        WHERE enrolling = @dist_ID
        UNION ALL
            SELECT
            t.distID as D, t.[Name] as N, t.enrolling as E, t.parent_code as P
        FROM sales_reps t
            INNER JOIN Hierarchy h ON t.enrolling = h.D
)
SELECT *, CASE WHEN P > 5 Then 1 Else 0 END As Q 
From Hierarchy

OK, so that’s not too big of a deal, the problem is now I need to use this data a lot more and will be doing multiple queries on this for a couple minutes. So the plan is to sometimes (in order to not break current functionality) create a table with this data that will be deleted later.

I’m trying something like:

CASE @temp_Table
    WHEN IS NULL
        Select * FROM retTable
    ELSE
        SELECT INTO @temp_Table
        * FROM retTable
End 

But I can’t get it to let me name the results retTable or like the case statement. I don’t think this is a proper use of a case statement, but I don’t know what else to use.

  • 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-06-09T12:56:37+00:00Added an answer on June 9, 2026 at 12:56 pm

    This is the best text I’ve found about how to share data between stored procedues. There is a lot of information in it but it’s worth a read:

    http://www.sommarskog.se/share_data.html

    Not knowing the full details of how often and in what scenarios you want to save this data to a table there are a few options you could use.

    If you’re only needing to save this information in one place, a simple INSERT-EXEC might work for you. You create a temp table in the calling procedure that matches the output of the called procedure (which I’m calling hierarchy_stored_proc)

    CREATE TABLE #results (...)
    INSERT INTO #results 
     EXEC hierarchy_stored_proc
    

    If you think you’ll need this output in more places, you could change the called procedure to always write data to the temp table, and then add a paramter to the proc that would indicate if it should return the result set via a select statement. If you make the default value of that procedure 1 then existing functionality shouldn’t be affected. Something like:

    WITH Hierarchy AS (...)
    INSERT INTO #results
    SELECT ...
    
    IF @returnResults=1
      SELECT * FROM #results
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure which returns sys_refcursor and I am trying to fetch
I have a stored procedure which takes an XML parameter and inserts the data
I have a stored procedure which returns a ref cursor as follows: CREATE OR
I have a stored procedure which update a table based on such calculation and
I have a stored procedure which accepts a User defined table type called SeasonTable
I have a stored procedure which basically does something like select top 1 expiryDate,
I have a stored procedure which is doing a lot of delete. Hundreds of
I have a stored procedure which executes another procedure inside it. The second one
I have a stored procedure which is called inside a trigger on Insert/Update/Delete. The
I have a stored procedure in which if I write the following query without

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.