存储过程在oracle中有错误

时间:2013-10-03 17:54:48

标签: oracle plsql

create or replace procedure placeorder(
  itemid in tempcart.item_id%type,num in integer,cid in customer_info.cust_id%type)
is
  cnt integer;
  amt integer;
  next_id number(5);
  temp tempcart%rowtype;
  cursor temp2 is select * from tempcart where tempcart.item_id=itemid;
  cursor temp1 is select * from tempcart;
BEGIN
  cnt:=0;
  select count(*) into cnt from porder;
  select max(oid) into next_id from porder;
  if(cnt=0) then
    next_id:=1;
  else
    next_id:=next_id+1;
  end if;
  if(num=1) then
    insert into corder values(next_id,itemid);
    select amount into amt from tempcart where item_id=itemid;
    insert into porder values(next_id,amt,sysdate); 
    open temp2;
    fetch temp2 into temp;
    insert into history values (itemid,temp.pid,temp.quantity,temp.amount,cid,temp.name);
  else
    amt:=0;
    open temp1;
    loop
      fetch temp1 into temp;
      exit when temp1%notfound;
      insert into corder values(next_id,temp.item_id);
      amt:=amt+temp.amount;
      insert into history values(temp.item_id,temp.pid,temp.quantity,temp.amount,cid,temp.name);
    end loop;
    insert into porder values(next_id,amt,sysdate);      
  end if;
end placeorder;

这个程序有什么问题?当我使用placeorder(1,1,2)调用它时,它只会在corder中插入值,但不会在porder中插入值。

1 个答案:

答案 0 :(得分:0)

抱歉,我无法重现这一点。

您没有向我们提供您正在使用的表格,也没有提供其中的任何数据,因此我使用了表格

CREATE TABLE customer_info (cust_id INTEGER);

CREATE TABLE tempcart (item_id INTEGER, pid INTEGER, quantity INTEGER, amount INTEGER, name VARCHAR2(4000));

CREATE TABLE corder (oid INTEGER, itemid INTEGER);

CREATE TABLE porder (oid INTEGER, amt INTEGER, order_date DATE);

CREATE TABLE history (itemid INTEGER, pid INTEGER, quantity INTEGER, amount INTEGER, cid INTEGER, name VARCHAR2(4000));

和数据

INSERT INTO tempcart (item_id, pid, quantity, amount, name) VALUES (1, 2, 3, 4, 'test 1');
然后我跑了

EXEC placeorder(1, 1, 2);

完成此操作后,我可以验证corderporder是否同时被插入,history表也是如此:

SQL> select * from corder;

       OID     ITEMID
---------- ----------
         1          1

SQL> select * from porder;

       OID        AMT ORDER_DAT
---------- ---------- ---------
         1          4 03-OCT-13

SQL> select * from history;

    ITEMID        PID   QUANTITY     AMOUNT        CID
---------- ---------- ---------- ---------- ----------
NAME
--------------------------------------------------------------------------------
         1          2          3          4          2
test 1

因此,很难看出你的问题是什么。

我注意到您没有关闭游标temp1temp2。尝试使用CLOSE temp1CLOSE temp2完成后立即关闭它们。