Sample stored procedure for converting finite-layer tree data to infinite-layer tree data (ms SQL 2 K)

Source: Internet
Author: User
Tags rtrim

-- Example stored procedure for converting finite-layer tree data into infinite-layer tree data (ms SQL 2 K)
If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [p_tree_convert] ') and objectproperty (ID, n' isprocedure') = 1)
Drop procedure [DBO]. [p_tree_convert]
Go

Set quoted_identifier on
Go
Set ansi_nulls off
Go

 

Create procedure p_tree_convert

-- Example stored procedure for converting finite-layer tree data to infinite-layer tree data

-- Result data.
Create Table [DBO]. [# tree_info] (
[Nodeid] [varchar] (10) null, -- tree Data Node Code
[Parentid] [varchar] (10) null, -- parent node code of the tree data node
[Nodename] [nvarchar] (255) Collate chinese_prc_ci_as null, -- node name
[Address] [nvarchar] (255) Collate chinese_prc_ci_as null, -- other node Information
[Icon] [int] Null -- node icon number
) On [primary]

-- Get the data of a finite-layer tree data node
Select * #
From (select dpcode1 as node, dpname1 as nodename
From ora_custdept
Where (dpcode2 = '00') and (dpcode3 = '000') and (dpcode4 = '000 ')
Union all
Select dpcode1 + dpcode2 as node, dpname2 as nodename
From ora_custdept
Where (dpcode3 = '000') and (dpcode4 = '000') and (dpcode2 <> '00 ')
Union all
Select dpcode1 + dpcode2 + dpcode3 as node,
Dpname3 as nodename
From ora_custdept
Where (dpcode3 <> '000') and (dpcode4 = '000') and (dpcode2 <> '00 ')
Union all
Select dpcode1 + dpcode2 + dpcode3 + dpcode4 as node,
Dpname4 as nodename
From ora_custdept
Where (dpcode3 <> '000') and (dpcode4 <> '000') and (dpcode2 <> '00 '))
Tree_node
Order by Node

Insert into # tree_info (nodeid, parentid, nodename)
-- Obtain the data of the parent node from the finite-layer tree data node and insert the result table.
Select node, case when Len (ltrim (rtrim (node) = 2 then '0' when Len (node)
= 4 then substring (ltrim (rtrim (node), 1, 2) When Len (node)
= 7 then substring (ltrim (rtrim (node), 1, 4) When Len (node)
= 10 then substring (ltrim (rtrim (node), 1, 7) end as parentid, nodename
From #

Select * from # tree_info -- view the result
 
Drop table #
Drop table # tree_info

Go
Set quoted_identifier off
Go
Set ansi_nulls on
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.