全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1699
推到 Plurk!
推到 Facebook!

treeview结构数据汇总

答題得分者是:Mickey
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-09-04 21:57:48 IP:219.140.xxx.xxx 未訂閱
公司資料0 ------公司001 ----------公司00100 ---------------公司0010000 ---------------公司0010001 ------公司002 ----------公司00200 ---------------公司0020000 ---------------公司0020001 ----------------------公司002000100 ----------公司00201 《公司表》 的树结构如上(公司后的数字为公司代码)  每个公司都有收入和支出 现在想计算出每个公司的盈利数字  每个公司的利润为 (自身收入-自身支出)+子公司的利润(子公司收入-子公司支出)    公司的业绩表现用 《总汇表》体现 我想通过查询出所有的非叶子节点 通过游标循环对每个非叶子节点进行汇总 我写了一个存储过程 如下  
create proc zonghui @time int
as
begin tran
declare @code varchar(20)
---------我觉得应该通过这一句来查询出所有的非叶子节点 但在下....
declare code  cursor for select 代码 from 公司表...
open code
fetch code into @code while @@fetch_status=-2
select @code=rtrim(@code) '%'
delete from 总汇表 where 时间=@time
insert into 总汇表(时间) values(@time)
update 总汇表 set 利润=b.利润 from 总汇表 as a,(select sum(isnull(收入)-isnull(支出)) as 利润 from 公司表 where 时间=@time and 公司代码like @code) as b where a.时间=@time
close code
deallocate code
commit
go
 
若按如上存储过程计算《总汇表》的公司利润 结果是所有公司利润为零 想来想去 觉得应该是 查询所有的非叶子节点 这个环节有问题 请大大指教 發表人 -
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-09-06 21:50:09 IP:220.228.xxx.xxx 未訂閱
沒有仔細看您的預存程序內容. 直覺的認為...你可以考慮用遞回(
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-09-07 09:46:53 IP:219.140.xxx.xxx 未訂閱
请问 Mickey    公司資料0 ------公司001 ----------公司00100 ---------------公司0010000 ---------------公司0010001 ------公司002 ----------公司00200 ---------------公司0020000 ---------------公司0020001 ----------------------公司002000100 ----------公司00201    如果以上面这个结构为例 具体怎么通过遞回(Recursive)来一層一層呼叫下去... 还请大大指点
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-09-09 15:13:05 IP:218.163.xxx.xxx 未訂閱
以下只是個遞迴程序的例子, 參考參考 :
create procedure TBLTPRCF @parent varchar(20),@tbltp varchar(1)
/* Base On SCHEMA_RELS Recursive Update SCHEMA_TBLS.TBL_TYPE */
as
begin
    declare @child varchar(20)
    UPDATE SCHEMA_TBLS set TBL_TYPE=@tbltp where rtrim(TBL_NAME)=rtrim(@parent)
    declare child_tbls scroll cursor 
       for 
         select distinct rtrim(CHILD) from SCHEMA_RELS r
         where rtrim(PARENT)=rtrim(@parent) and ISIDEN='Yes' 
               and rtrim(CHILD)<>rtrim(@parent) 
               and (select TBL_TYPE from SCHEMA_TBLS where TBL_NAME=r.CHILD)<>@tbltp
         order by rtrim(CHILD)
    open child_tbls
    fetch child_tbls into @child
    while (@@fetch_status=0)
      begin
          close child_tbls
          deallocate child_tbls
          exec TBLTPRCF @child,@tbltp
          declare child_tbls scroll cursor 
              for 
                select distinct rtrim(CHILD) from SCHEMA_RELS r
                where rtrim(PARENT)=rtrim(@parent) and ISIDEN='Yes' 
                      and rtrim(CHILD)<>rtrim(@parent)
                      and (select TBL_TYPE from SCHEMA_TBLS where TBL_NAME=r.CHILD)<>@tbltp
                order by rtrim(CHILD)
          open child_tbls
          fetch child_tbls into @child
      end
    close child_tbls
    deallocate child_tbls
end
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-09-16 21:41:45 IP:219.140.xxx.xxx 未訂閱
不好意思  耽误了好几天 SORRY    我仔细看了一下Mickey给出的例子 我想请问 /* Base On SCHEMA_RELS Recursive Update SCHEMA_TBLS.TBL_TYPE */ 中  SCHEMA_RELS 和SCHEMA_TBLS的表结构是怎样的    再者 自我感觉上 不好将其与treeview结构的数据汇总结合起来 此例关键在于使用“ exec TBLTPRCF @child,@tbltp” 体现循环(在下水平有限 望大大见谅) 请问大大 还有没有类似的关于 class="code"> create proc zonghui @time int as begin tran declare @code varchar(20) ---------我觉得应该通过这一句来查询出所有的非叶子节点 但在下.... declare code cursor for select 代码 from 公司表... open code fetch code into @code while @@fetch_status=-2 select @code=rtrim(@code) '%' delete from 总汇表 where 时间=@time insert into 总汇表(时间) values(@time) update 总汇表 set 利润=b.利润 from 总汇表 as a,(select sum(isnull(收入)-isnull(支出)) as 利润 from 公司表 where 时间=@time and 公司代码like @code) as b where a.时间=@time close code deallocate code commit go 该如何调整和修改 再次感谢各位大大
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-09-17 21:20:24 IP:219.140.xxx.xxx 未訂閱
请大大指点
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-09-19 22:04:27 IP:220.228.xxx.xxx 未訂閱
SCHEMA_RELS 資料檔中, 有一個 PARENT 與一個 CHILD 字段, 就像 TreeView 的 Parent 與 Child 的關係一樣, 你的預存程序, 應該有傳入的參數(如 "子公司代號"), 而這個參數應該會是 Cursor 的 where 條件之一, 因我最近也很忙, 恐怕無法"仔細"解說.    不過我想, 你應該可以"自力"做出來, 這樣才能有所收穫.
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-09-20 17:36:54 IP:219.140.xxx.xxx 未訂閱
多谢Mickey的回复     我再想一下 感觉有了点头绪(先前只是感觉parent and child有点类似母子公司的关系)     我一定会多尝试(在下水平有限 还请大大见谅) 若有不明之处 还请Mickey及各位大大指点    < >< >
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-09-23 19:08:26 IP:219.140.xxx.xxx 未訂閱
Mickey大大 你好 可能是我一直没有从 “declare code cursor for select 代码 from 公司表 where ... open code fetch code into @code while @@fetch_status=-2 select @code=rtrim(@code) '%'”的模式中走出来 反复试验还是无效 很遗憾 在《公司表》中 字段为:公司名称 公司代码 收入 支出 内部编号 公司資料0 ------公司001 ----------公司00100 ---------------公司0010000 ---------------公司0010001 ------公司002 ----------公司00200 ---------------公司0020000 ---------------公司0020001 ----------------------公司002000100 ----------公司00201 公司之间的关系是通过代码的层次性体现 每设定一层子公司 就在代码后加两个零 然后依次排序 且建立的方式 是通过先设定每个公司的绝对编号 然后再设定每个公司的母公司的绝对编号 同时设定其在该母公司下的顺序编号 还请Mickey 和 各位大大 帮忙看看 在下感激不尽
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-09-25 09:31:36 IP:220.228.xxx.xxx 未訂閱
我沒有環境, 所以都未經測試 :    
create proc zonghui @time int
as
begin 
  declare @root varchar(20)
  declare code cursor for select 代码 from 公司表 where datalength(代码)=1      delete from 总汇表 where 时间=@time
  insert into 总汇表(时间, 公司, 利润) select @time,代码,0 from 公司表      open code
  fech code into @root
  while (@@fetch_status=0)
    begin
      exec zonghui_rcf @time,@root
      fech code into @root
    end
  close code
  deallocate code
end
go    create proc zonghui_rcf @time int, @parent varchar(20) 
as
begin
  declare @code varchar(20),
          @benefit1 float,
          @benefit2 float
  declare code cursor for select 代码 from 公司表 where 代码<>@parent and 代码 like @parent '%'
  open code
  fech code into @code
  while (@@fetch_status=0)
    begin
      exec zonghui_rcf @time,@code
      fech code into @code
    end
  close code
  deallocate code      select @benefit1=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 代码=@parent
  select @benefit2=sum(利润) from 总汇表 where 时间=@time and 代码<>@parent and 代码 like @parent '%'      update 总汇表 set 利润=benefit1 benefit2 where 时间=@time and 代码=@parent    end
go
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#11 引用回覆 回覆 發表時間:2005-09-26 14:07:05 IP:219.140.xxx.xxx 未訂閱
我的环境是XP 数据库是SQL SERVER2000 我测试一下 我会尽快把结果报上来
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#12 引用回覆 回覆 發表時間:2005-09-26 15:34:19 IP:219.140.xxx.xxx 未訂閱
在SQL下检查语法 提示:错误207 列名‘benefit1’无效 列名‘benefit2’无效 另 我 在declare 中将benefit的类型 设为了money 错误同上 我觉得Mickey关于benefit的设置构思很好 一个用于计算该层公司利润 一个用于计算该层公司所有下级公司利润
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#13 引用回覆 回覆 發表時間:2005-09-26 22:11:04 IP:220.228.xxx.xxx 未訂閱
update 总汇表 set 利润=@benefit1 @benefit2 where 时间=@time and 代码=@parent 打錯字... 不過...這些應該要能...自行偵錯...加油喔
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#14 引用回覆 回覆 發表時間:2005-09-27 15:27:15 IP:219.140.xxx.xxx 未訂閱
按照Mickey大大告之的存储过程代码执行后 产生了两个存储过程zonghui和zonghui_rcf 执行存储过程后 《总汇表》里对应的时间和相应字段值皆为空    我对应的执行代码如下
     var
index:integer;
inputstring:string;
begin
 //取得当前时间的值
 setpara();
 index:=strtoint(para[2]);
 //提示输入要显示的时间
 inputstring:= InputBox('请选择需要统计的时间', '时间', inttostr(index-1));
 index:=strtoint(inputstring);
 //计算总汇表
 adocommand1.CommandText:='exec zonghui''' inputstring '''';   
adocommand1.Execute;
即按以上代码执行后 结果全部为空 我再检查对应时间 《公司表》中 的字段:公司代码 收入 支出 内部编号 有值 非空 另 我在 proc zonghui里 insert into 总汇表(时间, 公司, 利润) select @time,代码,0 from 公司表 后面 加上了 order by 内部编号 即 insert into 总汇表(时间, 公司, 利润) select @time,代码,0 from 公司表 order by 内部编号 在 proc zonghui_rcf里 select @benefit1=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 代码=@parent 后 我加上了 and 时间=@time 即 select @benefit1=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 代码=@parent and 时间=@time 目前自己水平较菜 但自己一定会努力 自从来到Ktop后 感觉成长了许多 这里的学术气氛很好 I LIKE IT 有的时候自己感觉前面是明显的障碍和困难 但自己总是偏执的相信 一定可以克服 在困难重重的处境中 坚持 再坚持下去 庆幸的是 这里有很多朋友实力超群 且有大侠风范 我觉得这很难得 我希望自己将来也能成为其中的一员 虽然现在问题仍未解决 但无论如何都很感谢Mickey及各位大大的关注和回复 对于困难重重 狭路相逢勇者胜----我信 發表人 - baby2321 於 2005/09/27 15:50:07 發表人 - baby2321 於 2005/09/27 15:52:30
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#15 引用回覆 回覆 發表時間:2005-09-28 09:28:23 IP:218.163.xxx.xxx 未訂閱
1.我沒有你的數據庫, 也不了解你資料結構, 無法幫忙 Debug. 2.建議你先不要直接用 Delphi/ADO 去調用預存程序, 先用 SQLServer 的 Query Analyzer 工具, 先確認預存程序的正確性. 3.預存程序中, 可以用 "print", 可以調看變數值.
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#16 引用回覆 回覆 發表時間:2005-09-29 21:07:23 IP:219.140.xxx.xxx 未訂閱
我再分析一下 有结果的话会马上跟各位大大汇报
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#17 引用回覆 回覆 發表時間:2005-10-30 19:12:11 IP:219.140.xxx.xxx 未訂閱
Mickey 很不好意思 断断续续耽误了好些时间 纠正了一些BUG 现将测试结果公布如下: 公司表字段:时间, 公司代码,公司名称, 收入,支出,parent(母公司代码) 总汇表字段:时间, 公司代码,公司名称, 利润 CREATE proc zonghui @time int as begin declare @root varchar(20) declare code cursor for select 代码 from 公司表 where datalength(代码)=1 delete from 总汇表 where 时间=@time insert into 总汇表(时间, 公司代码,公司名称, 利润) select @time,公司代码,公司名称,0 from 公司表 open code fetch code into @root while (@@fetch_status=0) begin exec zonghui_rcf @time,@root fetch code into @root end close code deallocate code end GO CREATE proc zonghui_rcf @time int, @parent varchar(20) as begin declare @code varchar(20),@benefit1 money,@benefit2 money declare code cursor for select 代码 from 公司表 where 代码<>@parent and 代码 like @parent '%' open code fetch code into @code while (@@fetch_status=0) begin exec zonghui_rcf @time,@code fetch code into @code end close code deallocate code select @benefit1=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 时间=@time and 代码=@parent select @benefit2=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 时间=@time and 代码<>@parent and 代码 like @parent '%' --此处我将原来的'总汇表'改为'公司表' update 总汇表 set 利润=@benefit1 @benefit2 where 时间=@time and 代码=@parent end GO 对应程序代码如下: procedure Tmain.N1Click(Sender: TObject); var index:integer; inputstring:string; newform:TReport; begin //取得当前时间 setpara(); index:=strtoint(para[2]); //提示用户输入要显示的时间 inputstring:= InputBox('请选择所要统计的时间', '时间', inttostr(index-1)); index:=strtoint(inputstring); adocommand1.CommandText:='exec zonghui''' inputstring ''''; adocommand1.Execute; newform:=TReport.Create(application); newform.SetPeriod(index); newform.QuickRep1.Preview;//在QuickRep1上我放置了两个QRDBText分别连接 总汇表 的 公司 和 利润 字段 end; 结果是:QuickRep1.Preview中(两个QRDBText)只显示了 一行总汇表的数据 利润仍然显示是0 查询数据库中的总汇表 利润 这个字段 全为0 感觉是不是在计算 '利润=@benefit1 @benefit2'出现问题 好象 总汇表 就停留在 ‘insert into 总汇表(时间, 公司代码,公司名称, 利润) select @time,公司代码,公司名称,0 from 公司表’ 的处理之后 我很不解 再次感谢Mickey大大赐教
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#18 引用回覆 回覆 發表時間:2005-11-03 19:24:52 IP:219.139.xxx.xxx 未訂閱
请问Mickey和各位大大 为什么在 公司表字段:时间, 公司代码,公司名称, 收入,支出,parent(母公司代码) 总汇表字段:时间, 公司代码,公司名称, 利润 里有数据的情况下 执行存储过程后 在总汇表里 利润 字段显示为零??    
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#19 引用回覆 回覆 發表時間:2005-11-18 02:14:52 IP:219.140.xxx.xxx 未訂閱
Mickey 你好 1.我重新建立一个数据库并在其中建立了 公司表 和 总汇表 字段和原来的公司表 总汇表一样 只不过 数据内容 是我临时写上去的(不多 只加了十来条数据) 再用您写的存储过程调试 结果竟然有数据!!!  
 
create proc zonghui @time int
as
begin 
  declare @root varchar(20)
  declare code cursor for select 代码 from 公司表 
--原来这里的where datalength(代码)=1应去掉 否则只有datalength(代码)=1的利润显示且显示值为零      delete from 总汇表 where 时间=@time
  insert into 总汇表(时间, 公司, 利润) select @time,代码,0 from 公司表      open code
  fech code into @root
  while (@@fetch_status=0)
    begin
      exec zonghui_rcf @time,@root
      fech code into @root
    end
  close code
  deallocate code
end
go    create proc zonghui_rcf @time int, @parent varchar(20) 
as
begin
  declare @code varchar(20),
          @benefit1 float,
          @benefit2 float
  declare code cursor for select 代码 from 公司表 where 代码<>@parent and 代码 like @parent '%'
  open code
  fech code into @code
  while (@@fetch_status=0)
    begin
      exec zonghui_rcf @time,@code
      fech code into @code
    end
  close code
  deallocate code      select @benefit1=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 代码=@parent
  select @benefit2=sum(利润) from 总汇表 where 时间=@time and 代码<>@parent and 代码 like @parent '%'      update 总汇表 set 利润=@benefit1 @benefit2 where 时间=@time and 代码=@parent    end
go
2.我把原来的公司表 总汇表导出到一个新建立的空数据库里 同样用您告之的存储过程调试 结果竟然还是 无 数据(应该说显示为零)!!! 相比较之下 原来的公司表 总汇表比我临时建立的公司表 总汇表 的数据内容多一些 但字段类型完全相同 我很困惑
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#20 引用回覆 回覆 發表時間:2005-11-18 21:38:34 IP:219.140.xxx.xxx 未訂閱
我把原表和我建的执行存储过程后有数据的临时表 表定义导出检查创建表的SQL语句 导出后结构是一样的
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#21 引用回覆 回覆 發表時間:2005-11-20 09:17:05 IP:220.228.xxx.xxx 未訂閱
很抱歉, 本人近來頗忙... 你一直只說"無值"...問題是...我也沒有你的數據庫阿 我想...你只能靠自己去偵錯了...之前不是有說了基本的偵錯方法... 例如加一行 "print @benefit1"....看看執行中的變數值
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#22 引用回覆 回覆 發表時間:2005-11-20 21:10:19 IP:219.140.xxx.xxx 未訂閱
公司表和总汇表.BAK 刚刚传到 會員求助程式檔案上傳區 【Delphi】公司表和总汇表执行存储过程后 无数据 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=81649 發表人 - baby2321 於 2005/11/20 21:19:04 ......自己刚才试着下载 提示找不到這個網頁 不知各位情况如何 發表人 - baby2321 於 2005/11/20 21:22:47
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#23 引用回覆 回覆 發表時間:2005-12-02 23:02:13 IP:219.140.xxx.xxx 未訂閱
痛苦非常
系統時間:2024-06-18 5:11:41
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!