COMMON TABLE EXPRESSIONS(cte): It is used best used to extract the hierarchical data and it is best explained with an example: Problem Many organizations have some type of hierarchy for business processes. When it comes to large organizations, the hierarchy can get very complex and large, so building a hierarchy in a RDBMS is a tedious task. We have to create views, cursors and so on, but using a CTE in SQL Server is a better solution to retrieve hierarchy-based data. Solution Common Table Expressions (CTE) have two types, recursive and non-recursive. We will see how the recursive CTE works with examples. A recursive CTE can be explained in three parts: Anchor Query: This is the first statement which is executed. This query will give the base data for the CTE. Separator: This is the middle part where in we generally use a UNION ALL and few more operators. Recursive Query: This is the main part, this is the CTE query which refers to the same CTE by recursion. ...
Comments
Post a Comment