1

我正在尝试使用 Forms 将一些信息简单地插入到 Oracle 中的表中。有时插入语句有效,有时无效。我只是没有足够的经验使用 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;
4

1 回答 1

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 表时使用它。

分享和享受。

于 2010-11-09T12:04:13.027 回答