有一表A,表结构如下 (多级分类)
 id,(自增)   flbm,(编码)     flmc(名称)
 1           010000        油品
 2           010100        汽油
 3           010101        汽油93#
010000为一级菜单
010100为二级菜单,(属于一级菜单010000)
010101为三级菜单(属于二级菜单010100)表 B  表结构如下 (此表为无限级分类)
 id,(自增序号) cname,(名称)parentid(父ID)  
我想把A表的数据导入到B表
请问怎么做

解决方案 »

  1.   

    declare @a table
    (
    id int identity(1,1),
    flbm char(6),
    flmc varchar(20)
    )
    insert into @a
    select '010000','油品' union all
    select '010100','汽油' union all
    select '010101','汽油93#'declare @b table
    (
    id int identity(1,1),
    cname varchar(20),
    parentid char(6)
    )
    insert into @b
    (
    cname,
    parentid
    )
    select
    flmc,
    Case
    when substring(flbm,5,2)<>'00' then left(flbm,4)+'00'
    when substring(flbm,3,2)<>'00' then left(flbm,2)+'0000'
    else '' end
    from
    @aselect * from @b/*
    id          cname                parentid 
    ----------- -------------------- -------- 
    1           油品                         
    2           汽油                   010000
    3           汽油93#                010100
    */
      

  2.   

    不是这样的 B表的parentid是int 型的表 B  表结构如下 (此表为无限级分类)
     id,(自增序号) cname,(名称,char()) parentid(父ID,int)  
    其实两各表都是多级分类表,只是方法不同
    我想把分类名称导过去,编码不要
      

  3.   

    按你的数据格式的话, 你的A表只是三级分类吧,每个非最底层类最多999个小类。
    一级类最多99类,这样,你的数据并不规范。
    如果是
     1           0010000        油品
     2           0010100        汽油
     3           0010101        汽油93#
    这样才规范。 否则,按一级只有2位用字串函数,left,substring之类的,对要操作的长度参数写了常量进去,以后你有别的值,语句又得重写了。这种语句的写法是不行的。
    就跟那天有个人问的
    AAA
    AAA_BBB
    AAA_BBB_CCC
    BBB
    一样,取没有子类的,一堆人用LEFT(A,3)来取,结果最后楼主说,长度是不定的。
      

  4.   

    --难道lz是要这样的?declare @a table
    (
    id int identity(1,1),
    flbm char(6),
    flmc varchar(20)
    )
    insert into @a
    select '010000','油品' union all
    select '010100','汽油' union all
    select '010101','汽油93#'declare @b table
    (
    id int identity(1,1),
    cname varchar(20),
    parentid char(6)
    )
    insert into @b
    (
    cname,
    parentid
    )
    select
    flmc,
    (select id from @a where flbm=
    Case
    when substring(a.flbm,5,2)<>'00' then left(a.flbm,4)+'00'
    when substring(a.flbm,3,2)<>'00' then left(a.flbm,2)+'0000'
    else '' end)
    from
    @a aselect * from @b/*
    id          cname                parentid 
    ----------- -------------------- -------- 
    1           油品                   NULL
    2           汽油                   1     
    3           汽油93#                2  
    */
      

  5.   

    对,就是 LouisXIV(夜游神)  这个意思
    我仔细看看
      

  6.   

    现在遇到个问题,不能用A表的ID
    A 表的ID 不是自增的,是32位的唯一标识,现在怎么办
      

  7.   

    B表的ID是自增的,不用管就是了。
      

  8.   

    那你把B表的parentid 类型设置成和A表的ID相同即可
      

  9.   

    我想舍弃A表的ID ,重新在B表生成新的ID,怎么办