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. ...
Popular posts from this blog
Transactions in SQL Server: Understanding Transactions in SQL Server Transactions are essential for maintaining data integrity, both for multiple related operations and when multiple users that update the database concurrently. What a transaction is When to use transactions Understanding ACID properties Design of a Transaction Transaction state Specifying transaction boundaries T-SQL statements allowed in a transaction Local transactions in SQL Server 2012 Distributed transactions in SQL Server 2012 Guidelines to code efficient transactions How to code transactions What Is a Transaction? A transaction is a set of operations performed so all operations are guaranteed to succeed or fail as one unit. Transaction is all or none A common example of a transaction is the process of transferring money from a checking account to a savings account. This involves two operations: Deducting money from the current account and Adding it to the savings...
Comments
Post a Comment