根据您使用的数据库,这可能纯粹通过服务器上的SQL来解决。这里有一个Firebird的例子(假设链接是一致的,并且没有可以通过引用完整性约束和触发器强制执行的无限递归 - 这里不包括这些)。
/* metadata */
set sql dialect 3;
create table test (
id integer not null,
val1 integer,
val2 integer,
next_id integer,
prev_id integer
);
alter table test add constraint pk_test primary key (id);
alter table test add constraint fk_test_next_id foreign key (next_id) references test (id) on update cascade;
alter table test add constraint fk_test_prev_id foreign key (prev_id) references test (id) on update cascade;
/* data */
insert into test (id, val1, val2, next_id, prev_id) values (1, 5, 3, 2, null);
insert into test (id, val1, val2, next_id, prev_id) values (2, 2, 4, 6, 1);
insert into test (id, val1, val2, next_id, prev_id) values (3, 1, 8, 4, null);
insert into test (id, val1, val2, next_id, prev_id) values (4, 1, 1, null, 3);
insert into test (id, val1, val2, next_id, prev_id) values (5, 8, 3, null, null);
insert into test (id, val1, val2, next_id, prev_id) values (6, 9, 3, null, 2);
/* update statement (could also be a stored procedure) */
execute block
as
declare variable id integer;
declare variable val1 integer;
declare variable val2 integer;
begin
for with recursive test_list (
id,
val1,
val2,
next_id
)
as (
select
t.id,
t.val1,
t.val2,
t.next_id
from test t
where (t.prev_id is null)
union all
select
t.id,
t.val1 + tl.val1,
t.val2 + tl.val2,
t.next_id
from test t
join test_list tl on (tl.id = t.prev_id)
)
select
tl.id,
iif(tl.next_id is null, tl.val1, 0) val1,
iif(tl.next_id is null, tl.val2, 0) val2
from test_list tl
order by tl.id
into
:id,
:val1,
:val2
do
update test set
val1 = :val1,
val2 = :val2
where (id = :id);
end
请在这里发表您的代码。不得不离开这个网站去其他地方阅读它很烦人,如果外部网站被移动或由于某种原因离线,它是不可用的。这还不算通过未来的读者检索。发布适用于您的问题在这里它的最小部分,请。谢谢。 – 2012-07-24 22:53:29
我没有看到你实际迭代表的位置..你需要在你的while循环之前有tblParts.first,在while循环的底部有tblParts.next。 – 2012-07-25 02:21:57
* lvBookmark:= tblParts.RecNo * - 为什么?标准方式,所以更好的图书馆支持,将是* lvBookmark:= tblParts.Bookmark * – 2012-07-25 09:17:32