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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:50:06+00:00 2026-06-11T06:50:06+00:00

I spent whole days and hours to investigate why cannot create temporary table when

  • 0

I spent whole days and hours to investigate why cannot create temporary table when I call a stored procedure from C# code.

Let’s assume a piece of C# code:

public void GetDataByFilter(string item, string filter ...) {
   ... 
   // provide stored procedure in new SqlCommand object
   sqlCommand.CommandType = CommandType.StoredProcedure;

   sqlCommand.Parameters.Add("@val1", aValue1);
   sqlCommand.Parameters.Add("@val2", aValue2);
   ...
   while(sqlDataReader.Read()) { /* process info here */ }
}

and my stored procedure

ALTER PROCEDURE [dbo].[aProcedure]
  @val1 VARCHAR(255),
  @val2 VARCHAR(255),
  ...
AS 
BEGIN
  IF OBJECT_ID(N'tempdb..##aTempTable') IS NULL
    CREATE TABLE tempdb..##aTempTable (
          id1 INT,
              id2 INT,
              id3 VARCHAR(255),
              ...
    )

ELSE 
    TRUNCATE TABLE tempdb..##aTempTable

    -- insert information into this table
    SELECT @paramList = ' /*list of parameters*/';

    EXEC sp_executesql @sql, @paramList, ... parameters
END

In C# code, in another function, I provided

sqlCommand.CommandText = "SELECT * FROM tempdb..##aTempTable";

But exception was thrown that tempdb..##aTemTable doesn’t exists.

If I execute stored procedure inside SSMS (Sql Server Management Studio) then the table is created.

The problem is also for TRUNCATE TABLE operation not only for CREATE TABLE.

I need temporary global table for to be used into different stored procedures accordingly with It is possible for created temporary table by a stored procedure to be used in another stored procedure?.

Any solution ?

  • 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-11T06:50:07+00:00Added an answer on June 11, 2026 at 6:50 am

    I believe that while it is possible to do what you’re describing, it is not too practical, as it presumes a lot of tricky timing: a session opens connection A and runs the proc to create the ##temp table, and that session keeps the connection open for as long as connections B, C, etc. need to be able to access the temp table.

    Yould’ be better off with a solution using a “permanent” table, where a row is added with a unique identifier (int or GUID) for every “session”, that value gets returned by the initialization call (or can otherwise be retrieved or identified), and used by all subsequent calls for the session.

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

Sidebar

Related Questions

I've spent whole days with PHP's DOM functions but i can't understand how it
I spent the last three days trying to create a small app using SDL
This case is really strange, i've spent 2 whole days to get Twitter Oauth
I've spent last 2 days trying to launch examples from Boost.Python with the ImportError:
I've got code that I am using from another team and I have spent
I've spent a few days looking for different solutions, but the whole area is
I spent days figuring and reading about how to produce an xml file from
I've a serious problem, and I cannot solve it by myself. I've spent hours
I spent the whole of last night searching for a free AspNet web chat
I've spent a whole day on this already without figuring it out. I'm hoping

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.