SQL Server Interview Questions on Temporary Tables

Q : What are the 2 types of Temporary Tables in SQL Server?

Ans : 1. Local Temporary Tables
2. Global Temporary Tables

Q : What is the difference between Local and Global Temporary Tables?

Ans : Local Temporary Tables:
1. Prefixed with a single pound sign (#).
2. Local temporary tables are visible to that session of SQL Server which has created it.
3. Local temporary tables are automatically dropped, when the session that created the temporary tables is closed.

Global Temporary Tables:
1. Prefixed with two pound signs (#).
2. Global temporary tables are visible to all the SQL server sessions.
3. Global temporary tables are also automatically dropped, when the session that created the temporary tables is closed.

Q : Can you create foreign key constraints on temporary tables?

Ans : No

Q : Do you have to manually delete temporary tables?

Ans : No, temporary tables are automatically dropped, when the session that created the temporary tables is closed. But if you maintain a persistent connection or if connection pooling is enabled, then it is better to explicitly drop the temporary tables you have created.
However, It is generally considered a good coding practice to explicitly drop every temporary table you create.

Q : In which database, the temporary tables get created?

Ans : TEMPDB database.

Q : How can I check for the existence of a temporary table?

Ans :
          

   
     
Leave a Reply
Name (required)
Email (will not be published) (required)
Web Site

List of Top Hospitals in India