A表,数据如下:
ID   strName intC    bType   strOrder
1 电脑 1 0 11
2 显示器 2 0 1121
3 主机 2 1 1122
4 主板 3 0 11221
5 CPU 3 0 11222如何更新为如下结果呢?ID   strName intC    bType   strOrder
1 电脑 1 0 11
2 显示器 2 0 1121
3 主机 2 1 1122
4 主板 3 1 11221(当上一层的bType为1时,子层跟住上一层变)
5 CPU 3 1 11222(当上一层的bType为1时,子层跟住上一层变)

解决方案 »

  1.   

    倒,格式怎么乱了,再发一次。A表,数据如下: 
    ID   strName intC    bType  strOrder 
    1    电脑    1   0   11 
    2    显示器   2    0    1121 
    3    主机    2    1    1122 
    4    主板    3    0    11221 
    5    CPU    3    0    11222 如何更新为如下结果呢? ID   strName intC    bType  strOrder 
    1    电脑    1    0    11 
    2    显示器   2    0    1121 
    3    主机    2    1    1122 
    4    主板    3    1    11221(当上一层的bType为1时,子层跟住上一层变) 
    5    CPU    3    1    11222(当上一层的bType为1时,子层跟住上一层变)
      

  2.   

    create table ceshi(id int,strname varchar(50),intc int,btype int,strorder varchar(10))
    insert into ceshi select 1,'电脑',  1,0,'11'
    insert into ceshi select 2,'显示器',2,0,'1121'
    insert into ceshi select 3,'主机',  2,1,'1122'
    insert into ceshi select 4,'主板',  3,0,'11221'
    insert into ceshi select 5,'CPU',   3,0,'11222'
    drop table ceshiupdate a set btype=b.btype from ceshi a join (
    select b.id,a.btype from ceshi a left join ceshi b 
    on a.id=b.intc where a.btype<>0)b on a.id=b.idselect * from ceshiid strname intc btype strorder
    1 电脑 1 0 11
    2 显示器 2 0 1121
    3 主机 2 1 1122
    4 主板 3 1 11221
    5 CPU 3 1 11222
      

  3.   

    create table ceshi(id int,strname varchar(50),intc int,btype int,strorder varchar(10))
    insert into ceshi select 1,'电脑',  1,0,'11'
    insert into ceshi select 2,'显示器',2,0,'1121'
    insert into ceshi select 3,'主机',  2,1,'1122'
    insert into ceshi select 4,'主板',  3,0,'11221'
    insert into ceshi select 5,'CPU',   3,0,'11222'
    drop table ceshiupdate a set btype=b.btype from ceshi a join (
    select b.id,a.btype from ceshi a left join ceshi b 
    on a.id=b.intc where a.btype<>0 and a.btype=1)b on a.id=b.idselect * from ceshi加上那个a.type=1就只更新是1的
      

  4.   

    好的,谢谢你,我在测试中.因为数据有少许差异.我开始以为要用到strOrder来实现.原来不需要的.
      

  5.   

    如果数据如下呢?是则上语句失效A表,数据如下: 
    ID   strName intC    bType  strOrder 
    1    电脑    1   0   11 
    2    显示器   2    0    1121 
    3    主机    2    1    1122 
    4    主板    1    0    11221 
    5    CPU    2    0    11222 如何更新为如下结果呢? ID   strName intC    bType  strOrder 
    1    电脑    1    0    11 
    2    显示器   2    0    1121 
    3    主机    2    1    1122 
    4    主板    1    1    11221(当上一层的bType为1时,子层跟住上一层变) 
    5    CPU     2    1    11222(当上一层的bType为1时,子层跟住上一层变)
      

  6.   

    create table ceshi(id int,strname varchar(50),intc int,btype int,strorder varchar(10))
    insert into ceshi select 1,'电脑',  1,0,'11'
    union all select 2,'显示器',2,0,'1121'
    union all select 3,'主机',  2,1,'1122'
    union all select 4,'主板',  3,0,'11221'
    union all select 5,'CPU',   3,0,'11222'declare @Index int
    set @Index=(select top 1 id from ceshi where btype=1)
    if @Index>0
    begin
    update ceshi
    set btype=1
    where
    id>@Index
    end
     select * from ceshidrop table ceshi
     发表于:2008-06-10 16:19:522楼 得分:0 
    SQL codecreate table ceshi(id int,strname varchar(50),intc int,btype int,strorder varchar(10))
    insert into ceshi select 1,'电脑',  1,0,'11'
    insert into ceshi select 2,'显示器',2,0,'1121'
    insert into ceshi select 3,'主机',  2,1,'1122'
    insert into ceshi select 4,'主板',  3,0,'11221'
    insert into ceshi select 5,'CPU',   3,0,'11222'
    drop table ceshiupdate a set btype=b.btype from ceshi a join (
    select b.id,a.btype from ceshi a left join ceshi b 
    on a.id=b.intc where a.btype<>0)b on a.id=b.idselect * from ceshi-----------------------------
    id strname intc btype strorder 
    1    电脑       1     0    11 
    2    显示器     2     0    1121 
    3    主机       2     1    1122 
    4    主板       3     1    11221 
    5    CPU       3     1    11222 
     
      

  7.   

    如果数据如下呢? 是则上语句失效 A表,数据如下: 
    ID   strName intC    bType  strOrder 
    1    电脑    1   0   11 
    2    显示器   2    0    1121 
    3    主机    2    1    1122 
    4    主板    1    0    11221 
    5    CPU    2    0    11222 如何更新为如下结果呢? ID   strName intC    bType  strOrder 
    1    电脑    1    0    11 
    2    显示器   2    0    1121 
    3    主机    2    1    1122 
    4    主板    1    1    11221(当上一层的bType为1时,子层跟住上一层变) 
    5    CPU     2    1    11222(当上一层的bType为1时,子层跟住上一层变)
      

  8.   

    能先解释下,intc和btype的含义不?怎么好象规律了 :)
      

  9.   

    我现在是依赖strOrder 进行排序的.
      

  10.   

    是的,最好是根据strOrder进行判断.intC是层数的意思,
    像电脑是第一层,
    显示器与主机是第二层,
    主板与CPU又是主机的第一二层.
    是一个典型的BOM结构.bType 是类型.
    ID   strName intC    bType  strOrder 
    1    电脑    1    0    11 
    2    显示器   2    0    1121 
    3    主机    2    1    1122 
    4    主板    1    1    11221(当上一层的bType为1时,子层跟住上一层变) 
    5    CPU     2    1    11222(当上一层的bType为1时,子层跟住上一层变)
      

  11.   

    create table ceshi(id int,strname varchar(50),intc int,btype int,strorder varchar(10))
    insert into ceshi select 1,'电脑',  1,0,'11'
    insert into ceshi select 2,'显示器',2,0,'1121'
    insert into ceshi select 3,'主机',  2,1,'1122'
    insert into ceshi select 4,'主板',  1,0,'11221'
    insert into ceshi select 5,'CPU',   2,0,'11222'
    drop table ceshiupdate a set btype=b.btype from ceshi a join (
    select b.id,a.btype from ceshi a left join ceshi b
    on charindex(a.strorder,b.strorder)>0 where a.btype=1)b on a.id=b.id
    select * from ceshi id strname intc btype strorder
    1 电脑 1 0 11
    2 显示器 2 0 1121
    3 主机 2 1 1122
    4 主板 1 1 11221
    5 CPU 2 1 11222
      

  12.   

    行了,谢谢你.因为自己对SQL不太在行,如果这样的语句.在数据多的情况下,效率高吗?我开始是想用For 来实现的.语句始下:'取得取最大层
    SQL = "Select Max(intC) From AAA"
    Set Rs = Conn.Execute(SQL,0,1) 
    '赋值给Y,最大层减一
    Y =  Rs(0)
    Y = Y - 1For I = 1 To Y 
        SQL = "Select strOrder From #AAA Where bType = 1 And intLayer = "&I 
        Set Rs = Conn.Execute(SQL,0,1) 
        Do While Not Rs.EOF 
          strOrder = Rs(0) 
          strOrderLen = Len(Rs(0)) 
          SQL = "upDate #AAA Set bType = 1 Where intLayer > "&I&" And Left(strOrder , "&strOrderLen&") = '"&strOrder&"' " 
          Conn.Execute(SQL) 
        Rs.MoveNext 
        Loop 
    Next
    Rs.Close 但以上代码,执行失败.相关贴子:http://topic.csdn.net/u/20080610/09/985b4db8-e98d-45b3-8bbd-4d21f6b7dca1.html