-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.

4

1 回答 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);

这样做之后,我可以验证corder并且porder都被插入,就像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 temp1and CLOSE temp2

于 2013-10-03T18:53:27.547 回答