table variable vs temp table in sql server code example
Example: sql server: difference between hashtable and table declared using declare keyword
There are quite differences like below.
1. Declarations: Table variables are declared as normal variables with type as table and columns along with their datatype.
whereas hashtables are created just like the normal tables.
Eg. i) Table Variable: Declare @tmpTable as Table(id Int, Name Varchar(500), Salary Decimal)
ii) Hashtable: Create Table(id Int, Name Varchar(500), Salary Decimal);
2. Next differences are where they are stored like Table variables are in Memory and Hashtables in tempdb.
3. As Hashtables are just like normal tables you can use them in transactions whereas not the table variables.
4. You can create indexes on Hashtables but not on table variables except the primary key indexes.
5. It's best to use table variables when you have small to moderate data to handle because of their better performance, as they reside in the memory.