首页 文章

存储过程在oracle中有错误

提问于
浏览
-1
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

    对不起,我无法重现这一点 .

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

    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 后,请在完成后立即尝试关闭它们 .

相关问题