Re: Tree table



Andrzej Jaworek (SPAMandrzejjaworekSPAM@xxxxx) writes:
I have a "tree" table:

Id - primary key
ParrentId - (foreign key) related to Id
Title
....

when I delete some record I want to delete it with all childs (cascade
deleting). I can't set cascade deleting on the same table :(. Is there
any easy way in the MSSQL 2005 to do this ? There is one idea - using
cursors + recursive functions but I think this solution is not easy
and elegant.

An INSTEAD OF trigger and a recursive CTE is the way to go:

CREATE TABLE hierarchy(id int NOT NULL PRIMARY KEY,
parent int NULL REFERENCES hierarchy(id))
go
CREATE TRIGGER hier_delete ON hierarchy INSTEAD OF DELETE AS
WITH CTE AS (
SELECT id, parent
FROM hierarchy
WHERE id IN (SELECT id FROM deleted)
UNION ALL
SELECT h.id, h.parent
FROM hierarchy h
JOIN CTE ON h.parent = CTE.id
)
DELETE hierarchy
FROM hierarchy h
JOIN CTE ON h.id = CTE.id
go
INSERT hierarchy(id, parent)
EXEC('SELECT 1, NULL
SELECT 10, 1
SELECT 11, 1
SELECT 12, 1
SELECT 20, 10
SELECT 21, 10
SELECT 110, 11
SELECT 111, 11
SELECT 112, 11
SELECT 120, 12
SELECT 1101, 110')
go
SELECT * FROM hierarchy ORDER BY id
DELETE hierarchy WHERE id IN (10, 12)
SELECT * FROM hierarchy ORDER BY id
go
DROP TABLE hierarchy


--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Hierarchyid is long, how to index?
    ... a vendor package. ... ALTER TABLE GENERIC_H ADD CONSTRAINT GENERIC_H_PK PRIMARY KEY ... becuase the table has more than one hierarchy per ... nested sets tree instead of doing the enumerated path? ...
    (comp.databases.ms-sqlserver)
  • Re: How to handle heirarchies in dimension attributes (Fairly green design question)
    ... Pro SQL Server 2000 Database Design - ... > represent a hierarchy in a dimension table, ... >> Pro SQL Server 2000 Database Design - ...
    (microsoft.public.sqlserver.datawarehouse)
  • Displaying hierarchical data
    ... I have a table in SQL Server, ... the hierarchy) ... Must support an unlimited number of sub-categories. ... are needed at the second level, 8 spaces at the second, 12 at the third, ...
    (microsoft.public.sqlserver.programming)
  • Relational Data & Datagrid
    ... I have a table in SQL Server, ... the hierarchy) ... Must support an unlimited number of sub-categories. ... are needed at the second level, 8 spaces at the second, 12 at the third, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: How do you think about OODBMS in .NET system?
    ... >I guessed about SQL Server, but it's too complex and needs more codes ... in RDBMS making hierarchy data is most hard work. ... OODBMS is a return to the Network DBMS with some OO features. ... But it is very easy to create your own serialization classes. ...
    (microsoft.public.dotnet.framework)