解决在Oracle Apex DB中获取用户输入的错误

klr1opcd  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(104)
CREATE OR REPLACE PROCEDURE RegEvent
AS
    Attender Attendee%rowtype;
    
BEGIN

    dbms_output.enable;
    --set prompt
    ACCEPT namee char prompt 'Enter your Name:  ';
    ACCEPT Attender_id char prompt 'Enter your id:  ';
    ACCEPT Attender_mail char prompt 'Enter your E-Mail:  ';
    ACCEPT Attender_phone char prompt 'Enter your Phone No:  ';
    ACCEPT Att_event prompt char 'Enter the event ID:  ';

    --Get Values

    namee := '&namee';
    Attender_Mail := '&Attender_Mail';
    Attender_phone := '&Attender_phone';

    dbms_output.put_line('Choose any id for future reference:  ');
    Att_event := '&Att_event';

    dbms_output.put_line('Enter the event want to register:  ');
    Attender_id := '&Attender_id';

    Attender.Attendee_name := namee;
    Attender.Attendee_mail := Attender_Mail;
    Attender.Attendee_phone := Attender_phone;
    Attender.Event_id := Att_event;
    Attender.Attendee_id := Attender_id;
    
    --Insert Table
    INSERT INTO
    attendee
    VALUES Attender;
    --Save Changes
    COMMIT;

END RegEvent;

它显示

Error at line 9: PLS-00103: Encountered the symbol "NAMEE" when expecting one of the following:

   := . ( @ % ;


7.     --set prompt
8.     ACCEPT namee char prompt 'Enter your Name:  ';
9.     ACCEPT Attender_id char prompt 'Enter your id:  ';
10.     ACCEPT Attender_mail char prompt 'Enter your E-Mail:  ';
11.     ACCEPT Attender_phone char prompt 'Enter your Phone No:  ';
zbsbpyhn

zbsbpyhn1#

嗯,这是相当混乱,你张贴,混合SQL Plus,匿名PL/SQL块,不能挽救。
不能将SQL Plus命令放入PL/SQL过程;利用他们另外,你用Apex标签标记了这个问题; SQL Plus命令在这里不起作用(如果您尝试使用accept,它将报告ORA-00900: invalid SQL statement)。
下面是它在SQL Plus中的工作原理。
样品表第一:

SQL> create table attendee
  2    (name   varchar2(10),
  3     id     number,
  4     mail   varchar2(20),
  5     phone  varchar2(10),
  6     event  number);

Table created.

过程接受参数,然后将其值插入目标表:

SQL> create or replace procedure regevent
  2    (par_name  in attendee.name%type,
  3     par_id    in attendee.id%type,
  4     par_mail  in attendee.mail%type,
  5     par_phone in attendee.phone%type,
  6     par_event in attendee.event%type
  7    )
  8  is
  9  begin
 10    insert into attendee (name, id, mail, phone, event)
 11      values (par_name, par_id, par_mail, par_phone, par_event);
 12  end;
 13  /

Procedure created.

获取值:

SQL> ACCEPT par_name  char prompt 'Enter your Name:  ';
Enter your Name:  Little
SQL> ACCEPT par_id    char prompt 'Enter your id:  ';
Enter your id:  123
SQL> ACCEPT par_mail  char prompt 'Enter your E-Mail:  ';
Enter your E-Mail:  [email protected]
SQL> ACCEPT par_phone char prompt 'Enter your Phone No:  ';
Enter your Phone No:  123-456
SQL> ACCEPT par_event char prompt 'Enter the event ID:  ';
Enter the event ID:  5588
SQL>

通过提供替代变量的值来调用过程;请注意单引号的使用(大约varchar2值):

SQL> set ver off
SQL> begin
  2    regevent('&par_name', &par_id, '&par_mail', '&par_phone', &par_event);
  3  end;
  4  /

PL/SQL procedure successfully completed.

测试结果:

SQL> select * from attendee;

NAME               ID MAIL                 PHONE           EVENT
---------- ---------- -------------------- ---------- ----------
Little            123 [email protected]         123-456          5588

SQL>

如果使用Apex,为什么不创建交互式报表(查看该表中的值)和表单(插入/编辑值)的组合?这就是Apex最好的用途。
如果你坚持使用SQL Workshop,那么创建一个过程,并通过立即提供值来调用它,而不要求用户(实际上,你自己-没有最终用户会看到SQL Workshop)提供值:

SQL> begin
  2    regevent('Foot', 421, '[email protected]', '9887-23', 43);
  3  end;
  4  /

PL/SQL procedure successfully completed.

SQL> select * From attendee;

NAME               ID MAIL                 PHONE           EVENT
---------- ---------- -------------------- ---------- ----------
Little            123 [email protected]         123-456          5588
Foot              421 [email protected]       9887-23            43

SQL>

相关问题