yanyyx

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

地区表结构

ID   ParentID Name

1   0  A1

2   1  A1-1

......

/* 临时表 */

create table #T_DQ(
  ID integer null,
  ParentID integer null,
  Name varchar(50) null,
  Grade integer null,  /* 级别 */
  State varchar(10) null  /* 1:未处理;2:已处理 */
)

/* SQL 为查询地区,包括子地区 */

Insert into #T_DQ (ID, ParentID, Name, Grade, State)
  select ID, ParentID, Name, 1, '1' from T_DQ where ParentID=@Area  /* 需要查询的地区 */

/* 循环插入子地区 */
Select @ID=Min(ID) from #T_DQ
while @ID is not null
begin
  Select @Grade=Grade from #T_DQ where ID=@ID
  Insert into #T_DQ (ID, ParentID, Name, Grade,State)
    select ID, ParentID, Name, @Grade+1, '1' from #T_DQ1 where ParentID=@ID
  Update #T_DQ set State='2' where ID=@ID
  Select @ID=Min(ID) from #T_DQ where State<>'2'
end

 

select * from #T_DQ

 

希望能对大家有帮助!

posted on 2009-01-04 14:43  Yanyyx  阅读(296)  评论(0编辑  收藏  举报