What is the difference between Temp Table and Table Variable?


Temp table and Table Variable both are created in TempDB and not in Memory

Syntax for creating Temp Table is exactly same as creating Physical Table

Create table  #Emp(Id int, EmpName Varchar(150),EmpAdd Varchar(150))

Insert into #Emp(Id,EmpName,EmpAdd) Values(1,'Ram','Bangalore')

Select *from #Emp

The 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:

Declare @Emp Table(Id int,EmpName Varchar(150),EmpAdd Varchar(150))

Insert into @Emp (Id,EmpName,EmpAdd) Values(2,'Ram','Bangalore')

Select *from @Emp


• Table variables can not have Non-Clustered Indexes
• You can not create constraints in table variables
• You can not create default values on table variable columns
• Statistics can not be created against table variables

Similarities with temporary tables include:

• Instantiated in tempdb
• Clustered indexes can be created on table variables and temporary tables
• Both are logged in the transaction log
• 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.

Table variable lifespan is only for the duration of transaction that it runs in. For executing the Table variable we have to execute the complete block code. Otherwise it will throw the exception.

When to use Table variable?

Table variable is mainly used in the function. We cannot use Temp table in function. For example

CREATE FUNCTION dbo.example1
(
)
RETURNS INT
AS
BEGIN
DECLARE @t1 TABLE (i INT)
INSERT @t1 VALUES(1)
INSERT @t1 VALUES(2)
UPDATE @t1 SET i = i + 5
DELETE @t1 WHERE i < 7

DECLARE @max INT
SELECT @max = MAX(i) FROM @t1
RETURN @max
END
GO


--Exec example1

--Select dbo.example1()


We cannot create above example using Temp Temple like this

CREATE FUNCTION dbo.example2
(
)
RETURNS INT
AS
BEGIN
CREATE TABLE #t1 (i INT)
INSERT #t1 VALUES(1)
INSERT #t1 VALUES(2)
UPDATE #t1 SET i = i + 5
DELETE #t1 WHERE i < 7

DECLARE @max INT
SELECT @max = MAX(i) FROM #t1
RETURN @max
END
GO

What is the CTE in sql server ?


1. CTE is the common table expression. This feature comes in sql server 2005 onward.
2. It is the temporary result set and generally it will be the result set of complex Sub Query.
3. CTE offer the same functionality as View.
4. It is defined with WITH Satement.
5. It improves the readability and ease in maintenance of complex queries and sub query.

CTE Simple Example


WITH CTE1
AS
 (
 SELECT 1 as Col1, 2 as Col2
 )
 
Select * from CTE1

Other Example

WITH CTEExample(EmpName,EmpAddress,DeptName) --Column Name for Temporary Table
AS
(
 Select E.EmpName, E.EmpAddress,D.DeptName from tblEmp E Join tblDept D On  E.DeptId=D.DeptId
)
Select * from CTEExample where CTEExample.DeptName='CS'

When to use CTE ?

1. To simplifying the complex query to simple one like View in sql
2. To maintain more readable code
3. To create recursive query.
4. if you need to reference/join the same data set multiple times you can do so by defining a CTE. Therefore, it can be a form of code re-use.
5. To hold the values as Temp Table.

Point to remember

CTE Life will expire after first select statement.