TAILIEUCHUNG - Hướng dẫn học Microsoft SQL Server 2008 part 47

Cây con các truy vấn Công việc chính của hệ thống phân cấp được trả lại hệ thống phân cấp như thiết lập một. Phương pháp được sử dụng danh sách kề phương pháp tương tự để quét lên hoặc xuống các hệ thống phân cấp. Không phải như vậy với con đường hiện thực. Tìm kiếm xuống một con đường hiện thực là một miếng bánh, nhưng tìm kiếm lên cây là một nỗi khổ thực sự. | Part III Beyond Relational Subtree queries The primary work of a hierarchy is returning the hierarchy as a set. The adjacency list method used similar methods for scanning up or down the hierarchy. Not so with materialized path. Searching down a materialized path is a piece of cake but searching up the tree is a real pain. Searching down the hierarchy with materialized path Navigating down the hierarchy and returning a subtree of all nodes under a given node is where the materialized path method really shines. Check out the simplicity of this query SELECT BusinessEntityID ManagerID MaterializedPath FROM WHERE MaterializedPath LIKE 1 263 Result BusinessEntityID ManagerID MaterializedPath 263 1 1 263 264 263 1 263 264 265 264 1 263 264 265 266 264 1 263 264 266 267 263 1 263 267 268 263 1 263 268 269 263 1 263 269 270 263 1 263 270 271 263 1 263 271 272 263 1 263 272 That s all it takes to find a node s subtree. Because the materialized path for every node in the subtree is just a string that begins with the subtree s parent s materialized path it s easily searched with a LIKE function and a wildcard in the WHERE clause. It s important that the LIKE search string includes the comma before the wildcard otherwise searching for 1 263 would find 1 2635 which would be an error of course. Searching up the hierarchy with materialized path Searching up the hierarchy means searching for the all the ancestors or the chain of command for a given node. The nice thing about a materialized path is that the full list of ancestors is right there in the materialized path. There s no need to read any other rows. Therefore to get the parent nodes you need to parse the materialized path to return the IDs of each parent node and then join to this set of IDs to get the parent nodes. The trick is to extract it quickly. Unfortunately SQL Server lacks a simple split function. There are two options build a CLR function that uses the C split function or build a T-SQL .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.