帖子1
帖子2这里在重新描述下描述1:我现在要对数据库内的很多的字段进行数据统计,
例如ID、实际充值总合、实际消费总合、活动赠金总合、活动赠金消费总合等等
涉及到库中的表消费记录表、用户表、单据表其中有一个字段记录用户是消费还是充值,例如flag1=0充值=1消费
还有一个字段记录方式,例如flag2=0同时flag1=0时,表示充值实际消费
方法有两种
一:我从消费表一次性取出所有数据,如ID=1的消费记录,然后对数据筛选(筛选时不涉及到数据库操作了)
二:使用sql语句传入不同的查询条件,分别对各个字段求和,每次都要操作数据库
其中哪种方法好呢??
如果有更好的方法,指点下小弟,谢谢~~~
如果只考虑传进一个ID用第一种方法打开数据库3次,第二种不用存储过程的话大概在12次左右
我统计了一下,按ID看消费表里面的数据,如果一次性取出,累计最多的只有一条100+,其余大概不到5个ID在40-50条记录之间,剩下的都在10条左右,这种情况用两种方法差别大吗?描述2:昨天大概扫了下存储过程的写法,有很多地方不会写!!!
自己写了一下,但感觉不对,请教一下应该怎么写??create or replace Procedure "GetInvoiceStat"(buyerID in nvarchar2(50),buyerID out nvarchar2(50),GiveInputMoney out NUMBER(10,2),GiveOutputMoney out NUMBER(10,2),GiveMoneyNow out NUMBER(10,2),MoneyNow out NUMBER(10,2),AllInvoiceMoney out NUMBER(10,2),...省略)
is 
begin
select buyerID into buyerID,MoneyNow into MoneyNow from buyerinfo a where a.buyerID=buyerID;
select sum(price) into AllInvoiceMoney from invoicelog b where b.buyerID=BuyerID;
select sum(price) into GiveInputMoney from payhistory c1 where c1.buyerID=buyerID and c1.category='1' and c1.flag2='PayRecordFlag2_10' group by buyerID;
select sum(price) into GiveOutMoney from payhistory c2 where c2.buyerid=buyerID and c2.category='2' and c2.flag3 in('8','9') group by buyerID;
...省略
return;
end;
这只是我要统计的一部分,其中要输出的字段buyerID ,GiveInputMoney ,GiveOutputMoney ,GiveMoneyNow ,MoneyNow ,AllInvoiceMoney ...省略
感觉这么写不对
要想得出结果上面的应该怎样改?????
而里面GiveMoneyNow =GiveInputMoney -GiveOutputMoney ,这在存储过程中怎么写???
如果传入参数buyerID不是单一的一个,而是多个,形式'id1','id2','id3'...
例如:第一句改为
select buyerID into buyerID,MoneyNow into MoneyNow from buyerinfo a where a.buyerID in (buyerID);
这种情况下应怎样修改存储过程??????

解决方案 »

  1.   

    本帖最后由 josy 于 2010-08-27 12:28:26 编辑
      

  2.   

    晕,你到网上找找Procedure的基础看一下吧,google上多呢,O(∩_∩)O~
      

  3.   


    基础昨晚看了点,麻烦看一下我的问题,红字部分,谢谢
    问题一:输出多个参数是这样@buyerID out nvarchar2(50),...省略,吗?
    问题二:set GiveMoneyNow into GiveMoneyNow  =GiveInputMoney -GiveOutputMoney 这样能输出GiveMoneyNow 吗?
    问题三:传入参数buyerID不是单一的一个,而是多个,形式'id1','id2','id3'...,例如:第一句改为
    select buyerID into buyerID,MoneyNow into MoneyNow from buyerinfo a where a.buyerID in (buyerID);
    这种情况下应怎样修改存储过程??????
      

  4.   

    oracle和sql server有区别,原来那写的sql server的
      

  5.   

    不好意思,我的问法有问题,让你绕到sql server去了.
    现在快疯了,传值由string  buyerID变成list<string>lstbuyerID更搞不定了.....
      

  6.   

    而里面GiveMoneyNow =GiveInputMoney -GiveOutputMoney ,这在存储过程中怎么写???
    GiveMoneyNow =case when GiveOutputMoney is null then GiveInputMoney else 
    GiveInputMoney -GiveOutputMoney end