Analysis of SQL Server temporary tables

Time:2021-9-19

Create temporary tables, # representing local temporary tables and ## representing global temporary tables. What are the specific meanings of local temporary tables and global temporary tables?

For example, it’s clearer to take a look at the local temporary table and enter the following text in the new query:


After running, we execute the input in this file: select * from #temptable. After execution, we can query the following data:

We also open a new query and name it new query 2. In new query 2, execute and enter: select * from #temptable. The prompt is as follows:

Modify the #temptable in the SQL statement to ##temptable, and execute it again according to the above method. It will be found that the error will not be prompted in the new query 2, and the same result as that in the new query 1 can be found. Why?=> The local temporary table is only visible in the current session; Global temporary tables are visible in all sessions.

Please note that after using the global temporary table, don’t forget to drop the table. However, if you use a local temporary table, you can not do this. At the end of the current session, the system will automatically recycle the created local temporary table.

For specific usage of temporary table, please refer tohttps://www.jb51.net/article/23952.htm