hierarchyid

Get last child from sql hierarchyid

坚强是说给别人听的谎言 提交于 2019-12-11 21:09:48
问题 I have hierarchyid in my tables like this. /1/ - This is Category /1/1/ - This is SubCategory /1/1/1/ - This is Item /1/1/2/ - Item /1/1/3/ - Item /1/2/ - SubCategory /1/2/1/ - Item /1/2/2/ - Item /1/2/3 - Item I want to get the last child regardless of Category, Subcategory or Items using SQL Query. For Eg. If its category, It will return /1/ ; If It's Subcategory It should return /1/ or /2/ (second child/last child); If it's Item it should return 3rd child/last child. I tried this query.

How to get recursivelevel using SQL Server 2012 hierarchyid?

空扰寡人 提交于 2019-12-11 07:14:22
问题 I know its quite challenging, Is there any sql specialist please kindly help me to solve this. I Have hierarchyID like below in NameHID column. Its representing /NameID/dadID/MomID/ respectively. Which means the students fatherid and the motherID. My Table Name is students. this is my sample NAMEHID column /2/8/5/ /5/11/12/ /8/7/9/ I need a output like NameID | RecursiveLevel 2 0 5 1 7 2 8 1 9 2 11 2 12 2 From this Pic you can see the what is the RecursiveLevel. This tree representing

Updating “Hierarchyid” in SQL Server

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 23:20:23
问题 I've used Hierarchyid data type in one of my tables in SQL Server. Now I want to change the father of one of the rows, but when I change that all its descendant HierarchyId's must change according to that. Is there a function to do that or I must change all of them myself. If I need to do that, what is the best way ? Thanks In advance 回答1: Use the "GetReparentedValue" Function to do it. This is what I did for a table in my database : DECLARE @FatherNode AS _INT_ SELECT @FatherNode = [PLC_PLC

What native dotNet data type is most appropriate for conveying SQL Server hierarchyId values?

半腔热情 提交于 2019-12-07 17:52:09
问题 Specifically we have a SQL Server stored procedure that accepts a hierarchyId as a parameter, and typically we have a SOAP layer on our stored procs that allows them to be called via SOAP. The SOAP services are implemented using the [WebMethod] attribute on C# methods, and these methods are defined using native dotNet types that typically map well to SQL server types. At this time I am not sure what data type to use in the C# WebMethod to accept a hierarchyId. Perhaps I should use a hierarchy

Find all leaf node records using hierarchyid

送分小仙女□ 提交于 2019-12-07 17:20:44
问题 How would I go about querying for only records that are "only leaf nodes" (ie. no children)? I have tried a query like this: select * from TableA tt where tt.HierarchyId.GetDescendant(null, null) not in ( Select t.HierarchyId from TableA t ) But this still seemed to return some nodes that had children. I am using the built-in hierarchyid data type (part of ms sqlserver) 回答1: SELECT A.HieracrchyId, A.HierarchyId.ToString() FROM dbo.TableA AS A LEFT OUTER JOIN dbo.TableA AS B ON A.HierarchyId =

Find all leaf node records using hierarchyid

一曲冷凌霜 提交于 2019-12-06 02:06:53
How would I go about querying for only records that are "only leaf nodes" (ie. no children)? I have tried a query like this: select * from TableA tt where tt.HierarchyId.GetDescendant(null, null) not in ( Select t.HierarchyId from TableA t ) But this still seemed to return some nodes that had children. I am using the built-in hierarchyid data type (part of ms sqlserver) SELECT A.HieracrchyId, A.HierarchyId.ToString() FROM dbo.TableA AS A LEFT OUTER JOIN dbo.TableA AS B ON A.HierarchyId = B.HierarchyId.GetAncestor(1) WHERE B.HierarchyId IS NULL; 来源: https://stackoverflow.com/questions/10694894

HierarchyID How to get all Parent from a child

筅森魡賤 提交于 2019-12-06 00:28:11
问题 i've a Problem with the hierarchyID and UserRights for a Menu. I only want to give the User the Right for Level 4 for example and my QUery should automaticly select all Parent from the Level 4 Child. How to do this ? Do you understand my Problem ? I simply want all Parents (ancestors) from a child. Greets Manuel 回答1: Something like this avoides the CTE SELECT t1.NodeId.ToString(), t1.Name FROM (SELECT * FROM test_table2 WHERE Name = 'Node 1.1.1') t2 , test_table2 t1 WHERE t1.NodeId = t2

Question about SQL Server HierarchyID depth-first performance

好久不见. 提交于 2019-12-05 22:11:11
问题 I am trying to implement hierarchyID in a table (dbo.[Message]) containing roughly 50,000 rows (will grow substantially in the future). However it takes 30-40 seconds to retrieve about 25 results. The root node is a filler in order to provide uniqueness, therefor every subsequent row is a child of that dummy row. I need to be able to traverse the table depth-first and have made the hierarchyID column (dbo.[Message].MessageID) the clustering primary key, have also added a computed smallint

What native dotNet data type is most appropriate for conveying SQL Server hierarchyId values?

安稳与你 提交于 2019-12-05 21:56:23
Specifically we have a SQL Server stored procedure that accepts a hierarchyId as a parameter, and typically we have a SOAP layer on our stored procs that allows them to be called via SOAP. The SOAP services are implemented using the [WebMethod] attribute on C# methods, and these methods are defined using native dotNet types that typically map well to SQL server types. At this time I am not sure what data type to use in the C# WebMethod to accept a hierarchyId. Perhaps I should use a hierarchy path string of the form '/1/2/3' and a function to parse this into a SqlHierarchyId that can be passed

Sql HierarchyId How do I get the last descendants?

99封情书 提交于 2019-12-05 05:14:12
Using t-sql hierarchy Id how do I get all of the rows that have no children (that is the last decendants)? Say my table is structured like this: Id, Name, HierarchyId And has these rows: 1, Craig, / 2, Steve, /1/ 3, John, /1/1/ 4, Sam, /2/ 5, Matt, /2/1/ 6, Chris, /2/1/1/ What query would give me John and Chris? Perhaps there are better ways but this seams to do the job. declare @T table ( ID int, Name varchar(10), HID HierarchyID ) insert into @T values (1, 'Craig', '/'), (2, 'Steve', '/1/'), (3, 'John', '/1/1/'), (4, 'Sam', '/2/'), (5, 'Matt', '/2/1/'), (6, 'Chris', '/2/1/1/') select * from