昨天我发的帖子~~
昨天大概扫了下存储过程的写法,有很多地方不会写!!!
自己写了一下,但感觉不对,请教一下应该怎么写??
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 ,这在存储过程中怎么写???

解决方案 »

  1.   

    将你的表结构 
    表内示例数据及想要的结果帖一下
    最好发到SQL版块去
    很快就应该可以得到参考答案
      

  2.   

    感觉语法不对吧...
    返回多个值 需要
    @res   int   output
      

  3.   

    这有个例子...
    http://blog.163.com/ysx_email/blog/static/1694213620106186514287/
      

  4.   

    @res int output??加到哪里??
    GiveMoneyNow(输出字段) =GiveInputMoney (输出字段)-GiveOutputMoney(输出字段) ,这在存储过程中怎么写???
    如果传入值buyerID改为'id1','id2'...这种形式应该怎么改??
      

  5.   

    using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ToString()))
    {
    conn.Open();
    SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
    MyCommand.CommandType = CommandType.StoredProcedure;
    MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
    MyCommand.Parameters["@a"].Value = 20;
    MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
    MyCommand.Parameters["@b"].Direction = ParameterDirection.Output;
    MyCommand.ExecuteNonQuery();
    Response.Write(MyCommand.Parameters["@b"].Value.ToString());
    http://topic.csdn.net/u/20091204/21/722689e1-7824-497c-b709-4b1118264633.html
      

  6.   

    sql 中的 set 没有使用过?
    set GiveMoneyNow(输出字段) =GiveInputMoney (输出字段)-GiveOutputMoney(输出字段)
      

  7.   

    create Procedure "GetInvoiceStat"
    (
    @GiveInputMoney int output
    略.....
    )
    as
    select @GiveInputMoney=sum(price) into GiveInputMoney from payhistory c1 where c1.buyerID=buyerID and c1.category='1' and c1.flag2='PayRecordFlag2_10' group by buyerID;
    略...
    go类似这样的吧...
      

  8.   

    那我在存储过程中怎么写呢?
    set GiveMoneyNow into GiveMoneyNow =GiveInputMoney (这是存储过程中得到的数据,后面是否应该加点什么??)-GiveOutputMoney(同GiveInputMoney )
      

  9.   

    thank you,太谢谢了,再问个问题,类似这样的存储过程
    当buyerID='id1','id2',...时
    可以使用select @GiveInputMoney=sum(price) into GiveInputMoney from payhistory c1 where c1.buyerID in  (buyerID )...这样的写法吗??数据能对的上吗??
    如果不行应该怎么改呢?
      

  10.   

    这是不是sql里的写法...这样在orcl中能正确运行吗......
      

  11.   


    create or replace Procedure GetInvoiceStat(buyerID in nvarchar2(,buyerID out nvarchar2,GiveInputMoney out NUMBER,GiveOutputMoney out NUMBER,GiveMoneyNow out NUMBER,MoneyNow out NUMBER,AllInvoiceMoney out NUMBER,...省略)
    as 
    begin
    select buyerID,buyerID into buyerID,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;
    给你提几点
    一存储过程名字不要双引号
    二参数没必要给它设个范围,自己找麻烦
    三is不是这里用的,应该为as
    四怎么会有两个into,如果需要,可以把字段查两遍
    五第三、四行sql语句你使用聚合函数了还用啥group by 可能还有其它问题没解决
      

  12.   


    更正:
    select @GiveInputMoney=sum(price) from payhistory c1 where c1.buyerID=buyerID and c1.category='1' and c1.flag2='PayRecordFlag2_10' group by buyerID;//应该是没有 into GiveInputMoney 的..
      

  13.   

    应该是没有 into GiveInputMoney 的..我昨晚看了几个orcl的存储过程事例,都是要加 into 输出字段的啊
      

  14.   

    一存储过程名字不要双引号
    二参数没必要给它设个范围,自己找麻烦
    三is不是这里用的,应该为as
    四怎么会有两个into,如果需要,可以把字段查两遍
    五第三、四行sql语句你使用聚合函数了还用啥group by  

    不太明白,我昨晚看了几个orcl的事例
    1:存储过程名字都加双引号
    2:参数没必要给它设个范围什么意思呢???
    3:orcl里貌似是用is的
    4:需要输出多个参数,最后很有可能是输出List<参数,参数...>这种样子的
    5:因为考虑到可能要改成buyerid in ('','',''....)的样子
      

  15.   

    你用oracle还是sql server? 各个数据库有些差异
      

  16.   


    oracle 中的变量赋值  是 :=eg:DECLARE Variable VARCHAR2(4000); Variable:='Sandy';
      

  17.   

    这个知道,可
    GiveMoneyNow:=GiveInputMoney-GiveOutMoney//后面这俩值是求出来的,怎么搞???
    还有我现在是传入一个buyerID,如果传入的是List<string>lstBuyerID这个怎么弄??
    还有我这现在是返回多个参数,而我希望是返回这多个参数的集合List<参数1,参数2...>怎么搞???
    快疯了 
      

  18.   

    GiveMoneyNow:=GiveInputMoney-GiveOutMoney//后面这俩值是求出来的,怎么搞???这个需要你通过sql 语句查询出来oracle 存储过程 可以返回数组的退一步, 不使用数组 你还可以使用  字符串拼接的方式 eg: 'buyerID1,buyerID2,buyerID3'
      

  19.   


    ...
    我理解力不强
    难道是这样??
    GiveMoneyNow:=(select sum(price) into GiveInputMoney from py_payhistory c1 where c1.buyerID=buyerID and c1.category='1' and c1.flag2='PayRecordFlag2_10' group by buyerID)-(select sum(price) into GiveOutMoney from py_payhistory c2 where c2.buyerid=buyerID and c2.category='2' and c2.flag3 in('8','9') group by buyerID);
    ....我后边还有一个公式是:x=a-b-c-d的那我不累死了还有
    oracle 存储过程 可以返回数组的退一步, 不使用数组 你还可以使用 字符串拼接的方式  eg: 'buyerID1,buyerID2,buyerID3'什么意思?不大明白....
    buyerID='buyerID1,buyerID2,buyerID3'??这样
    那我有多个(大概会有20条左右)统计查询
    select 输出字段 from biao where buyerID in (buyerID) group by buyerID
    我怎样把得出的这些字段按ID连接成完整的数据呢???
      

  20.   

    select sum(price) into GiveInputMoney from py_payhistory c1 where c1.buyerID=buyerID and c1.category='1' and c1.flag2='PayRecordFlag2_10' group by buyerID这条语句执行之后GiveInputMoney 就有值了你可以直接使用了所以你要做的只是先执行sql 语句select sum(price) into GiveInputMoney from py_payhistory c1 where c1.buyerID=buyerID and c1.category='1' and c1.flag2='PayRecordFlag2_10' group by buyerID;
    select sum(price) into GiveOutMoney from py_payhistory c2 where c2.buyerid=buyerID and c2.category='2' and c2.flag3 in('8','9') group by buyerID;然后 GiveMoneyNow:=GiveInputMoney-GiveOutMoney就可以了.多条的时候 ,你需要先学习一下 如何在存储过程中使用循环了解了之后,很容易的