Temp Tables usage and difference

There are two way's to create a temporary table in T-SQL:

declare @accounts table 
(
   account_id numeric(10,0) not null primary key
)

Or use this way 

create table #accounts 
(
   account_id numeric(10,0) not null primary key
)

The first manner is called a table variable and creates a table in pure memory, it is REALLY fast until you put to many records in it. At that moment it is a better idea to change the pure memory table to a table in the temp database a temporary table. Before you create a temp table in the temp database check if it not already exists:

IF OBJECT_ID('tempdb..#accounts') IS NOT NULL  DROP TABLE #accounts
create table #accounts 
(
   account_id numeric(10,0) not null primary key
)

Another advantage of the usage of the #temp table is the possibility to create an index, this will really speed up the performance of you query:

IF OBJECT_ID('tempdb..#accounts') IS NOT NULL  DROP TABLE #accounts
create table #accounts 
(
   account_id numeric(10,0) not null primary key
)
CREATE INDEX IDX_accounts_id ON #accounts(id)

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!