简单的预言机插入

发布于 2024-10-02 00:18:03 字数 1530 浏览 4 评论 0原文

我正在尝试使用 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;

I am trying to simply insert some information to a table in Oracle using Forms. Sometimes the insert statement works, sometimes it doesn't. I'm just not experienced enough with Oracle to understand what's not working. Here's the code:

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

篱下浅笙歌 2024-10-09 00:18:03

为了扩展 @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 表时使用它。

分享并享受。

To expand on @Mikpa's comment - it certainly appears that getting the values for ACCOUNTS.ANO and PASSUSE.LGN from sequences would be a good idea. Populating these fields automatically by using a trigger would also be helpful. Something like the following:

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

Having done the above you can now write your inserts into these tables as

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

and

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

Note that in the both statements the key values (PASSUSE.LGN and ACCOUNTS.ANO) are not mentioned in the field list as the new triggers should take care of filling them in correctly. Also note that when inserting into PASSUSE the RETURNING clause is used to get back the new value for LGN so it can be used in the insert into the ACCOUNTS table.

Share and enjoy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文