你好!我按照你的说法做出来一个,结果应该是正确的!你可以直接执行此段代码,验证结果。祝你好运!
use test
go
drop table usertable
go
create table usertable
(
站码 decimal(10,0) not null,
流域 char(20) not null,
水系 char(10) not null
)
go
insert into usertable values(10000011,'海河流域(片)','海水系')
insert into usertable values(10000012,'黄河流域(片)','王家坝水系')
insert into usertable values(10000016,'珠江流域(片)','花水系')
insert into usertable values(10000017,'海河流域(片)','海水系')
insert into usertable values(10000018,'海河流域(片)','海水系')
insert into usertable values(10000019,'测试流域(片)','测试水系')
insert into usertable values(41607600,'海河流域(片)','海水系')
go
select * from usertable
go
drop table target_table
go
create table target_table
(
t_nodeid int identity(1,1),
t_parentid int default 0,
stcd decimal(10,0) null ,
tnodetext char(20) not null
)
go
--第一步插入root内容
insert into target_table(stcd,tnodetext) (select min(站码),流域 from usertable group by 流域)
--第二步:
declare c1 cursor for
select 站码,流域,水系 from usertable --where 站码 not in (10000011,10000012,10000016,10000018)
declare @str1 decimal(10,0),@str2 char(20),@str3 char(10)
open c1
fetch next from c1 into @str1,@str2,@str3while @@fetch_status=0
begin
insert into target_table(t_parentid,stcd,tnodetext) (select t_nodeid,@str1,@str3 from target_table where tnodetext=@str2)
fetch next from c1 into @str1,@str2,@str3
end
close c1
deallocate c1
go
select * from target_table
go

解决方案 »

  1.   

    --查询
    select * from(
    select T_NODEID=(
    select count(distinct 流域) from 表
    where 站码<=a.站码
    ),T_PARENTID=0,*
    from(
    select 站码=min(站码),流域
    from 表 a 
    group by 流域
    )a
    union all 
    select T_NODEID=(
    select count(distinct 水系)  from 表
    where 站码<=a.站码
    )+(select count(distinct 流域) from 表)
    ,T_PARENTID=(
    select count(distinct 流域) from 表
    where 站码<=a.站码),*
    from(
    select 站码=min(站码),水系
    from 表 group by 水系
    )a
    )a order by T_NODEID
      

  2.   

    --测试--测试数据
    create table 表(站码 char(8),流域 varchar(20),水系 varchar(10))
    insert 表 select '10000011','海河流域(片)','海水系'
    union all select '10000012','黄河流域(片)','王家坝水系'
    union all select '10000016','珠江流域(片)','花水系'
    union all select '10000017','海河流域(片)','海水系'
    union all select '10000018','海河流域(片)','海水系'
    union all select '10000019','测试流域(片)','测试水系'
    union all select '41607600','海河流域(片)','海水系'
    go--查询
    select * from(
    select T_NODEID=(
    select count(distinct 流域) from 表
    where 站码<=a.站码
    ),T_PARENTID=0,*
    from(
    select 站码=min(站码),流域
    from 表 a 
    group by 流域
    )a
    union all 
    select T_NODEID=(
    select count(distinct 水系)  from 表
    where 站码<=a.站码
    )+(select count(distinct 流域) from 表)
    ,T_PARENTID=(
    select count(distinct 流域) from 表
    where 站码<=a.站码),*
    from(
    select 站码=min(站码),水系
    from 表 group by 水系
    )a
    )a order by T_NODEID
    go--删除测试
    drop table 表/*--测试结果
    T_NODEID    T_PARENTID  站码       流域                   
    ----------- ----------- -------- -------------------- 
    1           0           10000011 海河流域(片)
    2           0           10000012 黄河流域(片)
    3           0           10000016 珠江流域(片)
    4           0           10000019 测试流域(片)
    5           1           10000011 海水系
    6           2           10000012 王家坝水系
    7           3           10000016 花水系
    8           4           10000019 测试水系(所影响的行数为 8 行)
    --*/