|
6#
楼主 |
发表于 2008-3-13 15:31:05
|
只看该作者
以下是一个方法参考,不过较复杂,并且不能无限级的操作:
这种方法在SQL中利用存储过程实现了,但是依然非常不理想
DECLARE @s VARCHAR(1000)
DECLARE @id SMALLINT
set @s='1,'
DECLARE cur1 CURSOR FOR select [id] from list where parent=1
OPEN cur1 /***** 游标1 *****/
FETCH NEXT FROM cur1 INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
set @s=@s + convert(varchar,@id) + ','
DECLARE cur2 CURSOR FOR select [id] from list where parent=@id
OPEN cur2 /***** 游标2 *****/
FETCH NEXT FROM cur2 INTO @id
WHILE @@fetch_status=0
BEGIN
set @s=@s + convert(varchar,@id) + ','
DECLARE cur3 CURSOR FOR select [id] from list where parent=@id
OPEN cur3 /***** 游标3 *****/
FETCH NEXT FROM cur3 INTO @id
WHILE @@fetch_status=0
BEGIN
set @s=@s + convert(varchar,@id) + ','
-- 如果还有下一级就在此继续写:游标 4 , 5, 6 ... 照此类推
FETCH NEXT FROM cur3 INTO @id
end
CLOSE cur3
DEALLOCATE cur3
FETCH NEXT FROM cur2 INTO @id
END
CLOSE cur2
DEALLOCATE cur2
FETCH NEXT FROM cur1 INTO @id
END
CLOSE cur1
DEALLOCATE cur1
set @s=Left(@s,Len(@s)-1)
EXEC ('select weight=sum(weight) from list where parent in ('+ @s +')') /* 合计 */ |
|