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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:23:24+00:00 2026-06-15T21:23:24+00:00

I have 2 stored procedure with dynamic sql (ms sql 2005). First of them

  • 0

I have 2 stored procedure with dynamic sql (ms sql 2005). First of them create temp table (##Table) and fill data. Second procedure select data from temp-table created from first procedure.

first sp:

set @cmd = 'create table ' + @tableName +
                            '(ORDERS_DT_ID int' +
                            ...
                            ',WAREHOUSE_RESULT numeric(15, 3)) ' +

       ' insert into ' + @tableName + ' ( ORDERS_DT_ID, ... WAREHOUSE_RESULT )' +
       ' select ro.ORDERS_DT_ID, ... ro.WAREHOUSE_RESULT ' +
         ' from WH_REMAINS_BY_ORDER_FN ( ' + cast(@orderId as varchar(10)) + ' ) as ro '

exec (@cmd)      

second sp(for example):

set @cmd =  'select r.ORDERS_DT_ID, ... r.WAREHOUSE_RESULT' +
                     ' from ' + @tableName + ' as r'

exec (@cmd)

where @tableName – input parameter of sp ( = ‘##Table’, for example)

I use that procedures from asp.net mvc application. I call 1st procedure, check temp table from MSSMS (table is exist) then application are stopped on breakpoint (before 2nd sp), next i call 2nd procedure and table not exist (but between 1st and 2nd procedures call table is exist).

var result = dataContext.firstSP(table, orderId).FirstOrDefault(); // create temp table and fill data (+ return status)

var data = dataContext.secondSP( table, orderId ).AsQueryable(); // before execute we have temp table (check from MSSMS) and we have error after execute that code.

if we check temp table in 2nd procedure, we can see that table not exist:

if object_id ( N'tempdb.dbo.' + ltrim(@tableName) ) is null
begin
    exec dbo.firstSP @tableName, @orderId            
end

Why temp table from first sp dropped when we run second sp?

  • 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-15T21:23:26+00:00Added an answer on June 15, 2026 at 9:23 pm

    Global temporary tables are dropped as soon as the creating session ends and it is not being actively queried from another session.

    If you want to have control over the lifetime create a permanent table (possibly in tempdb)

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

Sidebar

Related Questions

I have to create a stored procedure using a Dynamic SQL solution because I
I have a messy stored procedure which uses dynamic sql. I can debug it
I have a dynamic SQL statement I've created in a stored procedure. I need
I have a Stored Procedure which executes some dynamic SQL. I want to use
I have a stored procedure which executes some dynamic sql, shown below. I've tried
I have a dynamic SQL which sits inside a stored procedure, but when I
I have a stored procedure which generates and executes a piece of dynamic T-SQL
I have dynamic sql in stored procedure: DECLARE @sql nvarchar(1000) SET @sql = 'UPDATE
I have a stored procedure that contains dynamic sql to return a result set.
I have a situation in a T-SQL stored procedure, where in a dynamic SQL,

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.