1。先建个临时表(id,name,fatherId)
#temp1
2。首先插入没有父节点的记录
insert #temp1 select top 1 * from tab where fatherid is null or fatherid = ''
3。insert #temp1 (select top 1 * from tab where fatherid = (select top 1 id from #temp1)and id not in (select id from #temp1))
4。循环第三步,直到找不出要插入 #temp1 的记录为止