简单的oracle插入

时间:2010-11-08 19:57:05

标签: oracle oracle10g oracleforms

我试图使用Forms将一些信息简单地插入到Oracle的表中。有时insert语句有效,有时则不然。我对Oracle的经验不足以了解什么不起作用。这是代码:

PROCEDURE create_account IS
temp_name varchar2(30);
temp_street varchar2(30);
temp_zip number(5);
temp_phone varchar2(30);
temp_login passuse.login%type;
temp_pass varchar2(30);
temp_total number(4);
temp_lgn passuse.lgn%type;
cursor num_cursor is
    select MAX(ano)
    from accounts;
cursor lgn_cursor is
    select MAX(lgn)
    from passuse;
BEGIN
temp_name:= Get_Item_Property('ACCOUNTS.A_NAME', database_value);
temp_street:= Get_Item_Property('ACCOUNTS.STREET', database_value);
temp_zip:= Get_Item_Property('ACCOUNTS.ZIP', database_value);
temp_phone:= Get_Item_Property('ACCOUNTS.STREET', database_value);
temp_login:= Get_Item_Property('PASSUSE.LOGIN', database_value);
temp_pass:= Get_Item_Property('PASSUSE.PASS', database_value);

open num_cursor;
fetch num_cursor into temp_total;

open lgn_cursor;
fetch lgn_cursor into temp_lgn;


if(lgn_cursor%found) then
    if(num_cursor%found) then
                    temp_lgn := temp_lgn + 20;
                    --the trouble maker..
                    INSERT INTO passuse (lgn, a_type, login, pass)
                    VALUES (temp_lgn, 1, temp_login, temp_pass);
                    temp_total := temp_total+1;
                    INSERT INTO accounts(ano,lgn,a_name,street,zip,phone)
                    VALUES (temp_total,temp_lgn,temp_name,temp_street,temp_zip,temp_phone);
    end if;
end if;
close lgn_cursor;
close num_cursor;
commit;

END;

1 个答案:

答案 0 :(得分:1)

要扩展@ Mikpa的评论 - 看起来从序列中获取ACCOUNTS.ANO和PASSUSE.LGN的值肯定是个好主意。使用触发器自动填充这些字段也会很有帮助。如下所示:

-- Note that the following is intended as a demo.  When executing these statements
-- you'll probably have to modify them for your particular circumstances.

SELECT MAX(ANO) INTO nMax_ano FROM ACCOUNTS;
SELECT MAX(LGN) INTO nMax_lgn FROM PASSUSE;

CREATE SEQUENCE ACCOUNTS_SEQ START WITH nMax_ano+1;
CREATE SEQUENCE PASSUSE_SEQ START WITH nMax_lgn+1;

CREATE TRIGGER ACCOUNTS_BI
  BEFORE INSERT ON ACCOUNTS
  FOR EACH ROW
BEGIN
  SELECT ACCOUNTS_SEQ.NEXTVAL
    INTO :NEW.ANO
    FROM DUAL;
END ACCOUNTS_BI;

CREATE TRIGGER PASSUSE_BI
  BEFORE INSERT ON PASSUSE
  FOR EACH ROW
BEGIN
  SELECT PASSUSE_SEQ.NEXTVAL
    INTO :NEW.LGN
    FROM DUAL;
END PASSUSE_BI;

完成上述操作后,您现在可以将这些插入内容写入

INSERT INTO passuse (a_type, login, pass)           
  VALUES (1, temp_login, temp_pass)
RETURNING LGN INTO temp_lgn;

INSERT INTO accounts(lgn, a_name, street, zip, phone)           
  VALUES (temp_lgn, temp_name, temp_street, temp_zip, temp_phone); 

请注意,在两个语句中,字段列表中未提及键值(PASSUSE.LGN和ACCOUNTS.ANO),因为新触发器应正确填充它们。另请注意,当插入PASSUSE时,RETURNING子句用于获取LGN的新值,因此可以在插入ACCOUNTS表中使用它。

分享并享受。