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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:16:34+00:00 2026-05-25T19:16:34+00:00

DECLARE @tmp_tab VARCHAR(20) SELECT @TMP_TAB = ‘#TMP_TAB_BANK’ + CAST(USER_ID(USER) AS NVARCHAR) + CAST(@@SPID AS

  • 0
DECLARE @tmp_tab VARCHAR(20)
SELECT  @TMP_TAB = '#TMP_TAB_BANK' + CAST(USER_ID(USER) AS NVARCHAR)
                                    + CAST(@@SPID AS NVARCHAR)

EXEC('CREATE TABLE ' + @TMP_TAB + (ID INT NULL, NAME VARCHAR NULL)')

//Break point

EXEC('select * from ' + @TMP_TAB)

I’m working in SQL Server 2005. In the code above I decide what to name my temp table. Then I create the temp table. If I run just these codes I receive Commands completed successfully message.

However if I execute the last line of code to retrieve the data (well, I know it’s empty) I get

invalid object name ‘#TMP_TAB_BANK157’

Why can’t I fetch records from a just created table? If the temp table was not created then why don’t I get any warning?

  • 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-05-25T19:16:35+00:00Added an answer on May 25, 2026 at 7:16 pm

    #TEMP tables are only available from the context they are created in. If you create #temp1 in one spid or connection, you can’t access it from any other scope, except for child scopes.

    If you create the #TEMP table in dynamic SQL, you need to select from it in the same scope.

    Alternatives are:

    • ##GLOBAL temp tables, which have their own risks.
    • Real tables
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DECLARE @T INT EXEC('SELECT @T = count(user_id) FROM ( .. .. .. )') PRINT
DECLARE @FINANCIALYEAR AS varchar(30) DECLARE @FINALFINANCIALYEAR AS int SELECT @FINANCIALYEAR=CONVERT(VARCHAR,YEAR(GETDATE())-2) + ', ' +
DECLARE @CURRENTSCHOOL TABLE (STUDENT VARCHAR(8), COURSE VARCHAR(8), SCHOOL VARCHAR(2)) INSERT INTO @CURRENTSCHOOL VALUES ('10000000','MCR1010','11')
DECLARE @OrdersTemp TABLE ( OrderId UNIQUEIDENTIFIER ) INSERT INTO @OrdersTemp SELECT ord.Id FROM Orders
Declare @BadDecimal varchar(5) Set @BadDecimal = '4.5' Declare @GoodDecimal Decimal Set @GoodDecimal = @BadDecimal
Given the following: declare @a table ( pkid int, value int ) declare @b
I want to declare a cursor on a table that does not exist. Of
I have query that looks like this declare @tmpTable Table(id int) insert into @tmpTable(id)
declare @string nvarchar(MAX) = '' How many chars are available in @string?
declare @xmlVal xml set @xmlVal = '<user> <token><id>ABC123</id><endDate>2013-06-16 18:48:50.111</endDate></token> <token><id>XYX456</id><endDate>2014-01-01 18:48:50.111</endDate></token> </user>' I need

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.