Invlaid Object Name on temp table

BKAN 41 Reputation points
2022-10-17T16:20:24.013+00:00

Hi All,
Not sure what is wrong with this SQL , I am getting an Invalid Object Name error
If Object_Id('tempdb.dbo.#Inter2') is not null Drop Table #Inter2
Insert Into #Inter2
select col1,col2

select * from #Inter2

I get Invalid Object name '#Inter2'. Please advice
Thanks

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,665 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 118.5K Reputation points
    2022-10-17T16:30:03.98+00:00

    Try another statement for inserting the data:

    select col1, col2  
    into #Inter2  
    from . . .  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. NikoXu-msft 1,916 Reputation points
    2022-10-18T01:48:02.9+00:00

    Hi @BKAN ,

    Do you want to copy data from a table and then insert the data into an existing table?
    If so, you can try the following code:

    INSERT INTO new_tablename  
    (column_name(s))  
    SELECT column_name(s)  
    FROM old_tablename;  
    

    Best regards
    Niko

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.