Monday, December 24, 2018

What are the Differences between the Temporary Table and Table Variable

Key Differences between the Temporary Table and Table Variable
The following are the Main differences between the Temporary Table and Table Variable.

Temporary Table
Table Variable
The Temporary Tables can be used in Stored Procedures, Triggers and Batches but not in User defined Functions.
The Table Variables can be used in User defined Functions, Stored Procedures, and Batches.
The Local Temporary Tables are Temporary Tables that are available only to the one Session in which we created them. Global Temporary Tables are Temporary Tables that are available to all sessions and all the users. 
The scope of the Table Variable in the stored procedure, user defined function or batch where it is declared like any local variable we create with a DECLARE statement. 
The Local Temporary Tables are automatically destroyed at the end of the Procedure or Session in which we created them. 
Global Temporary Tables are dropped automatically when the last session using the Temporary Table has been completed.
We can also drop Temporary Tables explicitly using Drop command similar to normal table.

The Table variables are automatically cleaned up at the end of the User defined Function, Stored Procedure, or Batch in which they are defined.
The Temporary Table name can be of maximum 116 characters.
The Table Variable name can be of maximum 128 characters.
The constraints like PRIMARY KEY, UNIQUE, CHECK, NULL etc can be implemented at the time of creating Temporary Tables using CREATE TABLE statement or can be added after the table has been created. FOREIGN KEY not allowed.
The constraints like PRIMARY KEY, UNIQUE, CHECK, DEFAULT, NULL can be added, but they must be incorporated with the creation of the table in the DECLARE statement. FOREIGN KEY not allowed. 
The Temporary Tables supports adding Indexes explicitly even after creation and it can also have the implicit Indexes which are the result of Primary and Unique Key constraint. 
The Table Variables doesn’t allow the explicit addition of Indexes after it is declared, the only means is the implicit indexes which are created as a result of the Primary Key or Unique Key constraint defined at the time of declaring Table Variable.
The Temporary Tables can also be directly created and data can be inserted using Select Into statement without creating a Temporary Table explicitly.
The Table Variables can’t be created using Select Into statement because being a variable it must be declared before use. 
The SET IDENTITY_INSERT statement is supported in Temporary Table.
The SET IDENTITY_INSERT statement is not supported in Table Variables.
We can’t return a Temporary Table from a user-defined function.
We can return a Table Variable from a user-defined function.
The Temporary Table can be truncated like normal table.
The Table Variables can’t be truncated like normal table or Temporary Tables.
The data in the Temporary Table will be rolled back when a transaction is rolled back similar to normal table. 
The data in the Table Variable will not be rolled back when a transaction is rolled back. 
The Temporary Tables used in Stored Procedures cause more recompilations of the stored procedures than when Table Variables are used. 
Table Variables used in stored procedures cause fewer recompilations of the stored procedures than when temporary tables are used.
The Temporary Table will generally use more resources than Table Variable.
The Table Variable will generally use less resources than a temporary table.
The Temporary Tables can be access in Nested Stored Procedures.
The Tables Variables can’t be access in Nested Stored Procedures.
The ALTER command can be used with Temporary Tables.
The ALTER command does not support with Table Variables.
The Temporary Tables should be used for large result sets.
The Table Variables should be used for small result sets and the everyday type of data manipulation since they are faster and more flexible than Temporary Tables.

--------------------------------------------------------------------------------------------------------
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog