Skip to content

Commit d8dd0f0

Browse files
authored
Merge pull request #7898 from pmasl/patch-331
Update from-transact-sql.md
2 parents 9be11f5 + 0aeb11a commit d8dd0f0

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

docs/t-sql/queries/from-transact-sql.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,15 @@ ON (p.ProductID = v.ProductID);
403403
## Using APPLY
404404
Both the left and right operands of the APPLY operator are table expressions. The main difference between these operands is that the *right_table_source* can use a table-valued function that takes a column from the *left_table_source* as one of the arguments of the function. The *left_table_source* can include table-valued functions, but it cannot contain arguments that are columns from the *right_table_source*.
405405

406-
The APPLY operator works in the following way to produce the table source for the FROM clause:
406+
The APPLY operator works in the following way to produce the table source for the FROM clause:
407407

408408
1. Evaluates *right_table_source* against each row of the *left_table_source* to produce rowsets.
409409

410-
The values in the *right_table_source* depend on *left_table_source*. *right_table_source* can be represented approximately this way: `TVF(left_table_source.row)`, where `TVF` is a table-valued function.
410+
The values in the *right_table_source* depend on *left_table_source*. *right_table_source* can be represented approximately this way: `TVF(left_table_source.row)`, where `TVF` is a table-valued function.
411411

412412
2. Combines the result sets that are produced for each row in the evaluation of *right_table_source* with the *left_table_source* by performing a UNION ALL operation.
413413

414-
The list of columns produced by the result of the APPLY operator is the set of columns from the *left_table_source* that is combined with the list of columns from the *right_table_source*.
414+
The list of columns produced by the result of the APPLY operator is the set of columns from the *left_table_source* that is combined with the list of columns from the *right_table_source*.
415415

416416
## Using PIVOT and UNPIVOT
417417
The *pivot_column* and *value_column* are grouping columns that are used by the PIVOT operator. PIVOT follows the following process to obtain the output result set:
@@ -573,32 +573,35 @@ FROM Sales.Customer TABLESAMPLE SYSTEM (10 PERCENT) ;
573573
```
574574

575575
### K. Using APPLY
576-
The following example assumes that the following tables with the following schema exist in the database:
577-
578-
- `Departments`: `DeptID`, `DivisionID`, `DeptName`, `DeptMgrID`
579-
580-
- `EmpMgr`: `MgrID`, `EmpID`
581-
582-
- `Employees`: `EmpID`, `EmpLastName`, `EmpFirstName`, `EmpSalary`
576+
The following example assumes that the following tables and table-valued function exist in the database:
577+
578+
|Object Name|Column Names|
579+
|---|---|
580+
|Departments|DeptID, DivisionID, DeptName, DeptMgrID|
581+
|EmpMgr|MgrID, EmpID|
582+
|Employees|EmpID, EmpLastName, EmpFirstName, EmpSalary|
583+
|GetReports(MgrID)|EmpID, EmpLastName, EmpSalary|
583584

584-
There is also a table-valued function, `GetReports(MgrID)` that returns the list of all employees (`EmpID`, `EmpLastName`, `EmpSalary`) that report directly or indirectly to the specified `MgrID`.
585+
The `GetReports` table-valued function, returns the list of all employees that report directly or indirectly to the specified `MgrID`.
585586

586-
The example uses `APPLY` to return all departments and all employees in that department. If a particular department does not have any employees, there will not be any rows returned for that department.
587+
The example uses `APPLY` to return all departments and all employees in that department. If a particular department does not have any employees, there will not be any rows returned for that department.
587588

588589
```sql
589590
SELECT DeptID, DeptName, DeptMgrID, EmpID, EmpLastName, EmpSalary
590-
FROM Departments d CROSS APPLY dbo.GetReports(d.DeptMgrID) ;
591+
FROM Departments d
592+
CROSS APPLY dbo.GetReports(d.DeptMgrID) ;
591593
```
592594

593-
If you want the query to produce rows for those departments without employees, which will produce null values for the `EmpID`, `EmpLastName` and `EmpSalary` columns, use `OUTER APPLY` instead.
595+
If you want the query to produce rows for those departments without employees, which will produce null values for the `EmpID`, `EmpLastName` and `EmpSalary` columns, use `OUTER APPLY` instead.
594596

595597
```sql
596598
SELECT DeptID, DeptName, DeptMgrID, EmpID, EmpLastName, EmpSalary
597-
FROM Departments d OUTER APPLY dbo.GetReports(d.DeptMgrID) ;
599+
FROM Departments d
600+
OUTER APPLY dbo.GetReports(d.DeptMgrID) ;
598601
```
599602

600603
### L. Using CROSS APPLY
601-
The following example retrieves a snapshot of all query plans residing in the plan cache, by querying the `sys.dm_exec_cached_plans` dynamic management view to retrieve the plan handles of all query plans in the cache. Then the `CROSS APPLY` operator is specified to pass the plan handles to `sys.dm_exec_query_plan`. The XML Showplan output for each plan currently in the plan cache is in the `query_plan` column of the table that is returned.
604+
The following example retrieves a snapshot of all query plans residing in the plan cache, by querying the `sys.dm_exec_cached_plans` dynamic management view to retrieve the plan handles of all query plans in the cache. Then the `CROSS APPLY` operator is specified to pass the plan handles to `sys.dm_exec_query_plan`. The XML Showplan output for each plan currently in the plan cache is in the `query_plan` column of the table that is returned.
602605

603606
```sql
604607
USE master;

0 commit comments

Comments
 (0)