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

  • Home
  • SEARCH
  • 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 8840953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:35:24+00:00 2026-06-14T10:35:24+00:00

I want to create a temporary table in a dynamic query and use it

  • 0

I want to create a temporary table in a dynamic query and use it afterwards. It will be created from a permanent table:

create table t (a integer);
insert into t values (1);

And the temp table creation is like this:

declare @command varchar(max) = '
    select *
    into #t
    from t
    ;
    select * from #t;
';
execute (@command);

When the @command is executed the select from the temporary table works.

Now if I select from the temporary table an error message is shown:

select * from #t;

Invalid object name '#t'

If the temporary table is created outside of the dynamic query it works:

select top 0 *
into #t
from t

declare @command varchar(max) = '
    insert into #t
    select *
    from t
';
execute (@command);

select * from #t;

Is it possible to persist a dynamically created temporary table?

  • 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-14T10:35:25+00:00Added an answer on June 14, 2026 at 10:35 am

    You can also create global temporary tables. For example, ##MyTemp.
    But, global temporary tables are visible to all SQL Server connections.

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

Sidebar

Related Questions

I want to insert a range of values into my table create TEMPORARY TABLE
i want to create a temporary table on the fly from a result set
I want to use SELECT INTO to make a temporary table in one of
I want to create a table variable or temporary table, using data from another
I am currently trying to use a bulk insert from a temporary table with
I want to create a temporary table, in which the columns will be those
I want to create temporary table in stored procedure and access it in the
I have created a temporary table using cakephp $this->Model->query() and inserted some records using
I'm using a stored procedure to create a temporary table and want to return
want to create a temporary table that has an auto_increment field plus a field

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.