Execute Stored Procedure into Temp Table

  • I would like to execute a stored procedure and have its results sent to a temp table so that I could do further actions on those results. For example, I could run this:

    execute sp_Get_Employees

    which could result in something like this:

    EmpID EmpName State

    100 Adams NY

    101 Baker RI

    102 Charles NY

    103 Davis NJ

    104 Edwards NY

    ...

    I don't know the correct syntax but I would like to process something like this:

    execute sp_Get_Employees into #Emps

    select * from #Emps where State = 'NY'

    Please let me know your suggestions for a valid set of statements that could do this type of processing. Thank you for your help.

  • gmrose (12/3/2015)


    I would like to execute a stored procedure and have its results sent to a temp table so that I could do further actions on those results. For example, I could run this:

    execute sp_Get_Employees

    which could result in something like this:

    EmpID EmpName State

    100 Adams NY

    101 Baker RI

    102 Charles NY

    103 Davis NJ

    104 Edwards NY

    ...

    I don't know the correct syntax but I would like to process something like this:

    execute sp_Get_Employees into #Emps

    select * from #Emps where State = 'NY'

    Please let me know your suggestions for a valid set of statements that could do this type of processing. Thank you for your help.

    You've only got 3 columns. Precreate the Temp Table and then do an INSERT/EXEC.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • you have to create the table explicitly, and then insert into the table; you cannot take advantage of the INTO #temp without jumping through extra hoops using openquery / openrowset

    CREATE TABLE #Emp(EmpID int, EmpName varchar(30) , State varchar(2) )

    INSERT INTO #Emp(EmpID ,EmpName,State)

    EXEC sp_Get_Employees

    openquery/rowset requires a static string, so you cannot pass paarameter s to a proc without resorting to dynamic sql

    SELECT * INTO #emp

    FROM OPENROWSET( 'SQLNCLI',

    'Server=(local);Trusted_Connection=yes;',

    'SET FMTONLY OFF; SET NOCOUNT ON; exec Db_Name.dbo.sp_Get_Employees'

    )

    gmrose (12/3/2015)


    I would like to execute a stored procedure and have its results sent to a temp table so that I could do further actions on those results. For example, I could run this:

    execute sp_Get_Employees

    which could result in something like this:

    EmpID EmpName State

    100 Adams NY

    101 Baker RI

    102 Charles NY

    103 Davis NJ

    104 Edwards NY

    ...

    I don't know the correct syntax but I would like to process something like this:

    execute sp_Get_Employees into #Emps

    select * from #Emps where State = 'NY'

    Please let me know your suggestions for a valid set of statements that could do this type of processing. Thank you for your help.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Since nobody mentioned it I will. You really should avoid using the sp_ prefix. You should use a different prefix, or even better drop the prefix entirely. http://sqlperformance.com/2012/10/t-sql-queries/sp_prefix

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Thank you both for your help. I found that inside the stored procedure was already code similar to that which you each proposed. When I entered the CREATE TABLE and INSERT/EXEC statements, the software told me that "An INSERT EXEC statement cannot be nested." I think I will have to abandon this issue. Thanks anyways.

  • gmrose (12/3/2015)


    Thank you both for your help. I found that inside the stored procedure was already code similar to that which you each proposed. When I entered the CREATE TABLE and INSERT/EXEC statements, the software told me that "An INSERT EXEC statement cannot be nested." I think I will have to abandon this issue. Thanks anyways.

    How many rows are typically loaded into the Temp Table in the stored procedure? I ask because it's a fairly trivial thing to change the Temp Tables to Table Variables, which would alleviate this problem.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I will try changing the #Emps temp table into a @Emps variable table. I have been called to work on another project now, so it may be some time before I can return to this. Thank you again.

  • gmrose (12/3/2015)


    I will try changing the #Emps temp table into a @Emps variable table. I have been called to work on another project now, so it may be some time before I can return to this. Thank you again.

    In that case, I'd like to suggest that you take 3 minutes and make some comments in your code. When you come back to it, you won't have to try to remember where you were at with the problem. Notes and comments in code can be wonderful things when used properly.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply