Search code examples
sql-servertemp-tables

how to select a temp table in database when debugging?


I create a temp table #temp1 in code, then insert the table in code. I want to select the table in sqlserver when debugging the code. but It can’t . sql server Prompt no talbe called the name . even in database tempdb. how to select a temp table in database when debugging?


Solution

  • insert into ##temp1 select * from TableName
    select * from ##temp1
    

    Explanation:

    We need to put "##" with the name of Global temporary tables. Below is the syntax for creating a Global Temporary Table:

    CREATE TABLE ##NewGlobalTempTable(
    UserID int,
    UserName varchar(50), 
    UserAddress varchar(150))
    

    The above script will create a temporary table in tempdb database. We can insert or delete records in the temporary table similar to a general table like:

    insert into ##NewGlobalTempTable values ( 1, 'Abhijit','India');
    

    Now select records from that table:

    select * from ##NewGlobalTempTable
    

    Global temporary tables are visible to all SQL Server connections. When you create one of these, all the users can see it.