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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:25:03+00:00 2026-06-01T05:25:03+00:00

I have a SelectSP which returns resultset that i am storing in the temp

  • 0

I have a SelectSP which returns resultset that i am storing in the temp table. Now i want to call another SP named InsertSP and pass the values from this temp table as parameter to the it.

Here is the code i have so far:

CREATE TABLE #tempTable
(
    Field1 VARCHAR(255),
    Field2 VARCHAR(255),
    Field3 VARCHAR(255),
    Field4 VARCHAR(255),
    Field5 VARCHAR(255),
    Field6 VARCHAR(255),
    Field7 VARCHAR(255)
        .
        .
        .

)

INSERT INTO #tempTable exec 
usp_SelectSP 'p1' ,'p2', 'p3'

Now i want to execute one more SP and pass Field1, Field2, Field3 of tempTable as parameter to that SP.

I was thinking something like this

usp_InsertSP
  @param1 = Field1
  @Param2 = Field2
  @Param3 = Field3

FROM #tempTable.

What is the best way to 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-06-01T05:25:05+00:00Added an answer on June 1, 2026 at 5:25 am

    If you can’t change the called SP then easiest way would probably be a cursor:

    DECLARE @f1 VARCHAR(255), @f2 VARCHAR(255) --, ...
    
    DECLARE c CURSOR 
      LOCAL STATIC READ_ONLY FORWARD_ONLY
      FOR SELECT Field1, Field2 --, ...
        FROM #tempTable;
    
    OPEN c;
    
    FETCH NEXT FROM c INTO @f1, @f2 --, ...
    
    WHILE @@FETCH_STATUS <> -1
    BEGIN
      EXEC usp_InsertSP
        @param1 = @f1,
        @param2 = @f2 --, ...
    
      FETCH NEXT FROM c INTO @f1, @f2 --, ...
    END
    
    CLOSE c;
    DEALLOCATE c;
    

    However I strongly recommend you change the called procedure. There is almost absolutely a more efficient way to deal with the data in this #temp table if you can change the called procedure to use it directly.

    EDIT

    Demonstrating that a called proc can see a #temp table created by the caller.

    CREATE PROCEDURE dbo.proc1
    AS
    BEGIN
        SET NOCOUNT ON;
    
        CREATE TABLE #t1(a INT);
    
        INSERT #t1 SELECT 1 UNION SELECT 2;
    
        EXEC dbo.proc2;
    
        DROP TABLE #t1;
    END
    GO
    
    CREATE PROCEDURE dbo.proc2
    AS
    BEGIN
        SET NOCOUNT ON;
    
        SELECT a FROM #t1;
    END
    GO
    
    EXEC dbo.proc1;
    

    Results:

    a
    -
    1
    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 6 columns in a table. I have a select query which selects
I have a simple procedure that selects 1000 rows from a table. For the
I want to build a query that finds elements that have the autofocus-attribute and
I have an (access) database table which contains data I would like to populate
I have written a php script that returns an arbitrary number of specific ids
I want to build a mySQL query, which returns all nodes in a graph
I have a webservice function that returns something like the following: New Orleans, Louisiana
I have a table in Sql Server 2008 Express which contains 18 million records.
I have a dynamic query which returns a DataTable. This DataTable will contain a
I have a pretty big SQL statement which returns a list of id's. I

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.