Hierarchyid れ螻 螳 轟螻 伎 螳讌.
- 螻豸旧 Data type
- 譟一蟲譟
- ろ
- 襦 ろ 讌
- 語 伎 覿襯
- 伎 螳 襷 蠏碁
- 豢
USE AdventureWorks
GO
--企
CREATE TABLE HumanResources.EmployeeOrg
(
OrgNode hierarchyid PRIMARY KEY CLUSTERED, --蠍一朱!
OrgLevel AS OrgNode.GetLevel(), --蠍一朱!
EmployeeID int UNIQUE NOT NULL,
EmpName varchar(20) NOT NULL,
Title varchar(20) NULL
) ;
GO
--碁煙れ(願鍵覦碁煙れ)
CREATE UNIQUE INDEX EmployeeOrgNc1
ON HumanResources.EmployeeOrg(OrgLevel, OrgNode) ;
GO
--襭語曙
INSERT HumanResources.EmployeeOrg (OrgNode, EmployeeID, EmpName, Title)
VALUES (hierarchyid::GetRoot(), 6, 'David', 'Marketing Manager') ;
GO
--蟆1
SELECT * FROM HumanResources.EmployeeOrg
--蟆2
SELECT
OrgNode.ToString() AS Text_OrgNode
, OrgNode
, OrgLevel
, EmployeeID
, EmpName
, Title
FROM HumanResources.EmployeeOrg
覿讌 曙(
GetDescendant()覃)
DECLARE @Manager hierarchyid
SELECT @Manager = hierarchyid::GetRoot()
FROM HumanResources.EmployeeOrg ;
INSERT HumanResources.EmployeeOrg (OrgNode, EmployeeID, EmpName, Title)
VALUES
(@Manager.GetDescendant(NULL, NULL), 46, 'Sariya', 'Marketing Specialist') ;
--蟆
SELECT
OrgNode.ToString() AS Text_OrgNode
, OrgNode
, OrgLevel
, EmployeeID
, EmpName
, Title
FROM HumanResources.EmployeeOrg
--蠏谿朱襦燕曙
CREATE PROC AddEmp(@mgrid int, @empid int, @e_name varchar(20), @title varchar(20))
AS
BEGIN
DECLARE @mOrgNode hierarchyid, @lc hierarchyid
SELECT @mOrgNode = OrgNode
FROM HumanResources.EmployeeOrg
WHERE EmployeeID = @mgrid
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
SELECT @lc = max(OrgNode)
FROM HumanResources.EmployeeOrg
WHERE OrgNode.GetAncestor(1) =@mOrgNode ;
INSERT HumanResources.EmployeeOrg (OrgNode, EmployeeID, EmpName, Title)
VALUES(@mOrgNode.GetDescendant(@lc, NULL), @empid, @e_name, @title)
COMMIT
END ;
GO
EXEC AddEmp 6, 271, 'John', 'Marketing Specialist' ;
EXEC AddEmp 6, 119, 'Jill', 'Marketing Specialist' ;
EXEC AddEmp 46, 269, 'Wanida', 'Marketing Assistant' ;
EXEC AddEmp 271, 272, 'Mary', 'Marketing Assistant' ;
EXEC AddEmp 6, 120, 'yasi', 'Marketing Specialist' ;
--蟆
SELECT
OrgNode.ToString() AS Text_OrgNode
, OrgNode
, OrgLevel
, EmployeeID
, EmpName
, Title
FROM HumanResources.EmployeeOrg
SELECT
OrgNode.ToString() AS Text_OrgNode
, hierarchyid::GetRoot() AS Text_Root
, OrgNode.GetAncestor(1) AS Text_Ancestor
, (SELECT EmpName FROM HumanResources.EmployeeOrg B
WHERE B.OrgNode = A.OrgNode.GetDescendant(0x78, NULL))
FROM HumanResources.EmployeeOrg A
WHERE EmpName = 'David'