Temporary TablesThere are two types of temporary tables:
Local Temporary Table: 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
You can create local temporary table adding # sign again the table name
Global Temporary Table: 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.
You can create global temporary table adding ## sign again the table name
What are the things you can do with the temporary tables?
-Add/drop constraints except foreign key
-You can perform DDL statements (Alter, Drop)
-Create clustered and non-clustered indexes
-Use identity columns
-Use it in transaction and it support transaction
-Perform any DML operations (SELECT, INSERT, UPDATE, DELETE)
-Create the table with same name using different session; make sure constraint name must be different in the table.
-- Adding the constraint primary key and unique key -- constraint will create the cluster and non-cluster index create table #temptbl (id int identity (100,1) Primary key, data varchar(20) constraint UK Unique ) insert into #temptbl values ('Jugal') insert into #temptbl values ('Jugal1') insert into #temptbl values ('Jugal2') select * from #temptbl sp_help #temptbl --Adding column to existing temporary table alter table #temptbl Add Name varchar(20) null -- Modifying column alter table #temptbl alter column name varchar(30) --adding index create nonclustered index UK2 on #temptbl (name) --checking indexes sp_helpindex #temptbl -- Supports transaction begin tran insert into #temptbl values('sqldbpool','sqldbpool') rollback select * from #temptbl --Checking for the foreign key create table #temptbl1 ( id int constraint FK1 references #temptbl1(id), value int ) --Skipping FOREIGN KEY constraint 'FK1' definition for temporary table. FOREIGN KEY constraints are not enforced on local or global temporary tables. insert into #temptbl1 values(2000,10) --DML Opertaions select * from #temptbl update #temptbl set data = 'SQLDBPool' where id = 100 select * from #temptbl delete from #temptbl where id = 101 select * from #temptbl --dropping temporary table drop table #temptbl
Table variableThe syntax for creating table variables is quite similar to creating either regular or temporary tables. The only differences involve a naming convention unique to variables in general, and the need to declare the table variable as you would any other local variable in Transact SQL. Life span of the table variable is limited to life of the transaction. You can only create the cluster index on table variable.
-- creating table variable declare @var table (id int identity(100,1) primary key, data varchar(20) default 'hi' ) --Checking DML Statement --working fine insert into @var values('jugal') insert into @var values('sqldbpool') delete from @var where id = 100 update @var set data = 'sqldbpool-1' where id = 101 select * from @var -- Checking transaction support/doesn't support transaction begin tran declare @var table (id int identity(100,1) primary key, data varchar(20) default 'hi' ) insert into @var values('jugal') insert into @var values('sqldbpool') insert into @var values (DEFAULT) delete from @var where id = 100 update @var set data = 'sqldbpool-1' where id = 101 rollback select * from @var -- Checking alter failed/doesn't support DDL alter table @var alter column data varchar(30) alter table @var Add Name varchar(20) null
Similarities between temporary tables and table variable:– Both are created in tempdb
– You can create constraint like primary key, default and check on both but the table variable has certain limitation for the default and check constrain where you can not use UDF
– Clustered indexes can be created on table variables and temporary tables
– Both are logged in the transaction log but the tempDB recovery model is SIMPLE, log will be truncated once the trasaction get complete.
Just as with temp and regular tables, users can perform all Data Modification Language (DML) queries against a table variable: SELECT, INSERT, UPDATE, and DELETE.
– You can not create non-cluster index and statistics on table variable but you can create it on temporary table.
– You can not use DDL statement on table variable but you can use it on temporary table.
– Table variable doesn’t support transaction wheras temporary table supports.