Problem
SQL Server 2005 introduced the APPLY operator, which is very much like a join clause and which allows joining between two table expressions i.e. joining a left/outer table expression with a right/inner table expression. The difference between join and APPLY operator becomes evident when you have a table-valued expression on the right side and you want this table-valued expression to be evaluated for each row from the left table expression.
Solution:
Cross Apply and Outer apply these two are allow us to join the table.
Cross Apply behaves like inner joins and Outter Apply behaves like Left outter join. But apply operator mostly used on table variables and DMV's (Dynamic Management Views and functions). Apply operator gives the most efficient result with table variables.
In inner join each check the condition each rows of both table and then it check the condition. Cross Apply returns those record which is present in the 2nd table as matched the condition. Whereas the OUTER APPLY operator returns all the rows from left table expression irrespective of its match with the right table expression. For those rows for which there are no corresponding matches in right table expression, it contains NULL values in columns of right table expression.
Example and Test cases:
Creating two table
1. Employee
2. Department
With following structure
create table Dept (DeptId int identity constraint pk_DeptId Primary key, DeptName varchar(25) ,Location varchar(25) )
create table Emp (EmpId int identity Constraint pk_EmpId primary key,Ename varchar(30),DeptId int constraint fk_DeptId_Dept references Dept(DeptId),Salary numeric(10))
I have inserted some data in both table but in Emp table I have insert some null data in DeptId column once I have firing command using Cross apply it will gives the result as like inner join
SELECT * FROM Dept D
cross apply
(
SELECT * FROM Emp E WHERE E.DeptId=D.DeptId
) Apply_demp
EmpId
|
Ename
|
DeptId
|
Salary
|
DeptId
|
DeptName
|
Location
|
2
|
Mandeep
|
1
|
25000
|
1
|
HR
|
Pune
|
3
|
Sandeep
|
2
|
25000
|
2
|
IT SW
|
Hyderabad
|
4
|
Amardeep
|
3
|
15000
|
3
|
Hardware
|
Chennai
|
6
|
Nitin
|
3
|
15000
|
3
|
Hardware
|
Chennai
|
But I don’t want to output like this means in this output some of the records are mismatch or not in result then it is not possible in such scenario so that we are using outer apply to display result for extracting output from Emp table in which DeptId is null
I am applying the outer apply then it will gives result as shown below
select * from Emp E
outer apply
(select * from Dept D where D.DeptId=E.DeptId) A
EmpId
|
Ename
|
DeptId
|
Salary
|
DeptId
|
DeptName
|
Location
|
1
|
Mangesh
|
NULL
|
15000
|
NULL
|
NULL
|
NULL
|
2
|
Mandeep
|
1
|
25000
|
1
|
HR
|
Pune
|
3
|
Sandeep
|
2
|
25000
|
2
|
IT SW
|
Hyderabad
|
4
|
Amardeep
|
3
|
15000
|
3
|
Hardware
|
Chennai
|
5
|
Akash
|
NULL
|
15000
|
NULL
|
NULL
|
NULL
|
6
|
Nitin
|
3
|
15000
|
3
|
Hardware
|
Chennai
|
The above scenario or our requirement we can get in this condition .
We can use table values function for implement this ,
Table Valued Function: This is the function which returns the result in the form of result set or table.
Firstly I have created following function
create function get_EmpDetails (@DeptId int)
returns table
as return(select *from Emp E where E.DeptId=@DeptId )
and applying outer and cross apply on it then the result may be as
select * from Emp E
outer apply get_EmpDetails(E.DeptId)
create function get_deptDetails(@DeptId int)
returns table
as
return (select * from Dept where DeptId=@DeptId)
Applying Cross Apply it will gives result as like on a table
select * from Emp E
outer apply get_EmpDetails(E.DeptId)
EmpId
|
Ename
|
DeptId
|
Salary
|
EmpId
|
Ename
|
DeptId
|
Salary
|
1
|
Mangesh
|
NULL
|
15000
|
NULL
|
NULL
|
NULL
|
NULL
|
2
|
Mandeep
|
1
|
25000
|
2
|
Mandeep
|
1
|
25000
|
3
|
Sandeep
|
2
|
25000
|
3
|
Sandeep
|
2
|
25000
|
4
|
Amardeep
|
3
|
15000
|
4
|
Amardeep
|
3
|
15000
|
4
|
Amardeep
|
3
|
15000
|
6
|
Nitin
|
3
|
15000
|
5
|
Akash
|
NULL
|
15000
|
NULL
|
NULL
|
NULL
|
NULL
|
6
|
Nitin
|
3
|
15000
|
4
|
Amardeep
|
3
|
15000
|
6
|
Nitin
|
3
|
15000
|
6
|
Nitin
|
3
|
15000
|
No comments:
Post a Comment