(Original) based on the tree structure data stored in the chain of the relational database system, calculate the algorithm of all the nodes of the subtree under a node (t-SQL language implementation)

Source: Internet
Author: User

(Original) based on the tree structure data stored in the chain of the relational database system, calculate the algorithm of all the nodes of the subtree under a node (t-SQL language implementation) 

Create Function f_subtree (@ dpcode varchar (9)/* tree structure data, which is used to find the User-Defined Function of all the nodes of the subtree under a node */
Returns @ new table ([dpcode] [varchar] (9), -- Node Code
[Dpname] [varchar] (20), -- node name
[Dpcode_p] [varchar] (9) -- parent node code of this node
)
Begin
Declare @ temp table ([dpcode] [varchar] (9), -- Node Code
[Dpname] [varchar] (20), -- node name
[Dpcode_p] [varchar] (9) -- parent node code of this node
) -- Temporary data storage table
Declare @ t table ([dpcode] [varchar] (9), -- Node Code
[Dpname] [varchar] (20), -- node name
[Dpcode_p] [varchar] (9) -- parent node code of this node
) -- Intermediate temporary table
Declare @ TT table ([dpcode] [varchar] (9), -- node Encoding
[Dpname] [varchar] (20), -- node name
[Dpcode_p] [varchar] (9) -- parent node code of this node
) -- Intermediate exchange temporary table

Delete @ temp
Delete @ t
Delete @ TT
Delete @ new

Insert into @ temp select * From dpet
Insert into @ t select * From @ temp where dpcode_p = @ dpcode
Insert into @ new select * From @ t -- result temporary table
While (exists (select * From @ t where dpcode in (select dpcode_p from @ temp )))
-- The loop is stopped only when all nodes on a layer are leaves.
Begin
Insert into @ TT select * From @ t -- intermediate exchange temporary table
Delete @ t
Insert into @ t select * From @ temp where dpcode_p in
(Select dpcode from @ TT)
Delete @ TT
Insert into @ new select * From @ t
End
Insert into @ new select * From @ temp where dpcode = @ dpcode

Return
End

------------------------------------------------- (The following is the implementation of the stored procedure)

Create Table dpet (-- Data Storage table structure of tree structure data
[Dpcode] [varchar] (9), -- Node Code
[Dpname] [varchar] (20), -- node name
[Dpcode_p] [varchar] (9) -- parent node code of this node
) On [primary]

/* Ms SQL 2000 is debugged. The structure of the table temp, T, new, and TT is consistent with that of the table dpet */

Create procedure desc_dept (@ dpcode varchar (9)
-- Tree structure data, finding all the nodes of the subtree under a node
Truncate table temp
Truncate table t
Truncate table TT
Truncate table new
Insert into temp select * From dpet -- temporary data storage table
Insert into T select * from temp where dpcode_p = @ dpcode -- intermediate temporary table
Insert into new select * from t -- result temporary table
While (exists (select * from t where dpcode in (select dpcode_p from temp )))
-- The loop is stopped only when all nodes on a layer are leaves.
Begin
Insert into TT select * from t -- intermediate exchange temporary table
Truncate table t
Insert into T select * from temp where dpcode_p in
(Select dpcode from TT)
Truncate table TT
Insert into new select * from t
End
Insert into new select * from temp where dpcode = @ dpcode
Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.