Database consultancy: tree-like structure
Subject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:
So, the main purpose is to display the names of the tree structure a request for T-SQL. Starting from MS SQL 2005, is the design on the T-SQL with the name of “common table expressions (common table expression – CTE)», which allows us to implement linear recursion (sorry of course that there is a cascade of recursion, but having the opportunity to linear recursion, we can already with many problems cope). In general, the recursive CTE syntax is that we have to announce the “anchor” point from which to start the recursion, and by the construction association «UNION ALL» add a query that uses the name of our CTE – a request refers to itself, of course, do not forget the second request to limit the output condition of recursion. To solve our problem, we need to “nesting depth – the level (LEVEL)», for that would understand how much to indent each line (space we do with functions or SPACE or REPLICATE – who like more). And also we need to compute the field that will do the sorting when displaying. The first thing that comes to mind is to form the current position and the entire list of managers, for that you can execute next query:
Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.
MoreSubject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:
MoreIn our time of greatest prevalence of databases were relational databases, which are the main targets of the tables and the relationships between them. Tables can solve most problems for data storage and manipulation. But in the real world entity requiring storage is not always presented in a tabular form. One of these very common types of data structures other than the table is a tree structure, where each data element is the parent and the offspring. An example of such a structure may be the structure of state enterprises, which is headed by the director (the root of the tree), his deputies, heads of departments from which are subject to certain deputies, employees of departments, which are subject to the rulers.
MoreSubscribe to our blog
Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.
Subject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:
Discuss your project with us