How to delete a temporary table in SQL Server?
In SQL Server, temporary tables can be dropped using the following methods:
- Use the DROP TABLE statement to delete a temporary table. For example:
DROP TABLE #temp_table;
- If a temporary table is created within a stored procedure or batch process and is automatically deleted at the end of the stored procedure or batch process, you do not need to use any statements to delete the temporary table. The scope of the temporary table is limited to the execution of the stored procedure or batch process.
Important note:
- The name of a temporary table must start with “#” symbol.
- Temporary tables are only accessible within the session in which they were created, and will be automatically dropped when the session ends.
- Temporary table definitions and data are stored in the tempdb database, rather than in user databases.