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时,子层跟住上一层变)
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时,子层跟住上一层变)
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时,子层跟住上一层变)
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
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的
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时,子层跟住上一层变)
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
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时,子层跟住上一层变)
像电脑是第一层,
显示器与主机是第二层,
主板与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时,子层跟住上一层变)
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
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