top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Temp tables in SQL server?

+6 votes
425 views

What are temp tables? What is the difference between global and local temp tables?

posted Nov 18, 2013 by Atul Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+4 votes

Temporary tables are just that. They are used most often to provide workspace for the intermediate results when processing data within a batch or procedure. They are also used to pass a table from a table-valued function, to pass table-based data between stored procedures or, more recently in the form of Table-valued parameters, to send whole read-only tables from applications to SQL Server routines, or pass read-only temporary tables as parameters. Once finished with their use, they are discarded automatically.

Temporary tables come in different flavours including, amongst others, local temporary tables (starting with #), global temporary tables (starting with ##), persistent temporary tables (prefixed by TempDB..), and table variables.(starting with (@)

Local temporary tables will remain till one session only, and once you close the session they will be deleted. They will be visible in your session only.

Global temporary tables are visible to any user, but once they all will close there instance it will drop automatically. While persistent temporary tables will be visible to all and you need to drop it explicitly.

answer Nov 19, 2013 by Neeraj Pandey
+1 vote

Temporary Tables are a great feature that lets you store and process intermediate results by using the same selection, update, and join capabilities that you can use with typical SQL Server tables.

The temporary tables could be very useful in some cases to keep temporary data. The most important thing that should be known for temporary tables is that they will be deleted when the current client session terminates.

Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server. Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.

answer Nov 18, 2013 by Jagan Mishra
...