What is Temporary Tables, Table Variable, CTE, Derived Tables and SubQueries?
Temp Table:
Types of Temporary Tables:
SQL Server contain two types of Temporary tables:
- Local Temporary Tables
- Global Temporary Tables
Local Temporary Tables:
Local temporary tables are temporary tables that are available only to the session that created them. These tables are automatically destroyed at the termination of the procedure or session that created them. Local temp tables are only available to the current connection for the user. Names of Local temp tables start with(“#”) hash sign.Local temporary tables are visible only in the current session, and can be shared between nested stored procedure calls.Example:
- Create local temp table.
- CREATE TABLE# TEMP_SAVE
- (
- Emp_IdINT,
- Emp_NameVARCHAR(30),
- Project_NameVARCHAR(30)
- )
- Insert data into temp table.
- INSERT INTO # TEMP_SAVE
- SELECT e.Emp_Id, e.EmployeeName, p.Project_NameFROMdbo.Employee e
- INNERJOIN
- dbo.Project p
- ON
- e.Emp_Id = p.Project_Id
Global Temporary Tables:
Create a Global temp table
- Create Table## TEmp
- (
- IIDintidentity(1, 1),
- Namenvarchar(50),
- Salaryint
- )
When to Use Temporary Tables
- We required to hold data from further query.
- When we have a complex Join operation.
- In some cases it may be a replacement of cursors.
Table Variable:
Table variables are somewhat like temp tables, but table variables are not SQL Server objects like temp tables. That is, you do not create a table variable with a create statement.
A table variable is also created in TempDB. The scope of a table variable is the batch, stored procedure, or statement block in which it is declared.
This acts like a variable and exists for a particular batch of query execution.
It gets dropped once it comes out of the batch.
This is also created in the tempdb database but not the memory.
This also allows you to create a primary key, identity at the time of Table variable declaration but not non-clustered index. They can be passed as parameters between procedures. Table variables are best suited for working with small row sets.
GO DECLARE @TProduct TABLE ( SNo INT IDENTITY(1,1), ProductID INT, Qty INT ) --Insert data to Table variable @Product INSERT INTO @TProduct(ProductID,Qty) SELECT DISTINCT ProductID, Qty FROM ProductsSales ORDER BY ProductID ASC --Select data Select * from @TProduct --Next batch GO Select * from @TProduct --gives error in next batch
CTE:
Example:
- WITH CTE(Emp_ID, Emp_Name, Project_Name)
- AS
- (
- SELECT e.Emp_Id, e.EmployeeName, p.Project_NameFROMdbo.Employee e INNERJOIN dbo.Project p ON e.Emp_Id = p.Project_Id
- )
- SELECT * FROM CTE
When to use CTE
- Use for recursive query.
- CTE is easy to implement compared to complex queries which involves several sub-queries.
Derived Table:
Example:
- SELECT * FROM
- (SELECT e.Emp_Id,e.EmployeeName,p.Project_Name FROM dbo.Employee e
- INNERJOIN
- dbo.Project p
- ON
- e.Emp_Id=p.Project_Id)Tab
- WHERE Tab.Emp_Id%2=1
When to Use:
- When you are required to perform some complex query and result of inner query work as table for outer query.
Subquery:
A subquery is a SELECT statement that is nested within another statement. Subquery is also known as nested query. Result of subquery is used in the main query as a condition to further restrict the data to be retrieved. The subquery used in select, update, delete and insert command.
Example:
- SELECT * FROM dbo.Emp e
- WHERE e.Id IN(SELECT e2.Emp_Id fromdbo.Employee e2);
When to use:
- When you require that inner query or condition must be fulfilled before generating any result.
- Use subquery when the result that you want requires more than one query and each subquery provides a subset of the table involved in the query.
- If the query requires a NOT EXISTS condition, then you must use a subquery because NOT EXISTS operator only work with subquery.
Note
Temp Tables are physically created in the tempdb database. These tables act as the normal table and also can have constraints, an index like normal tables.
CTE is a named temporary result set which is used to manipulate the complex sub-queries data. This exists for the scope of a statement. This is created in memory rather than the Tempdb database. You cannot create an index on CTE.
Table Variable acts like a variable and exists for a particular batch of query execution. It gets dropped once it comes out of a batch. This is also created in the tempdb database but not the memory.
Post a Comment
0 Comments