例如表department中有字段deptid,parentid。当删除一个父结点时,删除它的其下的所有子结点。
注:此方法只能在SQL Server 2005中使用,SQL Server 2000不支持。
WITH tempdept AS
( SELECT root.deptid, root.parentid
FROM department root
WHERE deptid=1
UNION all
SELECT sub.deptid, sub.parentid
FROM department sub, tempdept super
WHERE sub.parentid = super.deptid
)
delete from department where deptid in
( select deptid from tempdept )
删除后查看是否正确。已测试。
select * from department
删除父子结构表中数据SQL语句
最新推荐文章于 2024-09-11 06:57:25 发布