遇到异常时插入的plsql

遇到异常时插入的plsql,sql,oracle,plsql,Sql,Oracle,Plsql,编写PL/SQL程序(匿名块)在dept表中插入具有以下值的新部门记录。 (空,'LOGISTICS','SINGAPORE')。检查是否能够成功插入具有这些值的行。如果未成功,请处理限制插入行的错误(使用非预定义的异常处理)。 异常处理部分应该从表中找到最大deptno,将其递增1,并使用它插入新记录。 我不知道如何插入最大部门编号+1以及“物流”和“新加坡”的新记录。 该值应为(max(deptno)+1、'LOGISTICS'、'SINGAPORE') 我的密码 declare e_ins

编写PL/SQL程序(匿名块)在dept表中插入具有以下值的新部门记录。 (空,'LOGISTICS','SINGAPORE')。检查是否能够成功插入具有这些值的行。如果未成功,请处理限制插入行的错误(使用非预定义的异常处理)。 异常处理部分应该从表中找到最大deptno,将其递增1,并使用它插入新记录。 我不知道如何插入最大部门编号+1以及“物流”和“新加坡”的新记录。 该值应为(max(deptno)+1、'LOGISTICS'、'SINGAPORE')

我的密码

declare
e_insert_excep exception;
pragma exception_init(e_insert_excep,-01400);
begin
insert into dept values(NULL,'LOGISTICS','SINGAPORE');
exception
when e_insert_excep then insert into dept(deptno) select max(deptno)+1 from dept;
dbms_output.put_line(sqlerrm);
end;

您可以
插入。。。如果这是您要求的,请选择…
多列

...
INSERT INTO dept
            (deptno,
             <the other targeted columns>)
            SELECT max(deptno) + 1,
                   'LOGISTICS',
                   'SINGAPORE'
                   FROM dept;
...
。。。
插入部门
(德普诺,
)
选择最大值(deptno)+1,
"物流",,
“新加坡”
来自部门;
...
但是最好用一个自动递增的ID来定义表。看看如何实现这一点。

使用
max(field)+1
来生成唯一的值是一个非常糟糕的主意。你应该使用一个序列。若您严格地需要一个并没有间隙的值,那个么您应该创建一个包含这些值的表,并从中恢复它们

您想要做的事情可以很容易地使用SQL完成,其他答案已经说明了这一点。无论如何,如果您想在PL/SQL上执行此操作,并应用问题上所述的逻辑:

1.pragma的错误代码为-1400(不带前导0) 2.在异常发生后,您需要做一些事情,要么引发错误,要么不引发错误。 3.您需要在pragma异常后恢复max+1以插入所需内容

declare
e_insert_excep exception;
pragma exception_init(e_insert_excep,-1400);
v_depto dept.deptno%type;
begin
    insert into dept values(NULL,'LOGISTICS','SINGAPORE');
    commit;
    exception
    when e_insert_excep then 
       select max(deptno)+1 into v_depto from dept;
       -- again the insert
       insert into dept values(v_depto,'LOGISTICS','SINGAPORE');   
       commit;
       null; -- I don't want to raise an error, therefore I set null;
    when others then 
      dbms_output.put_line(sqlerrm);
      rollback; -- In case you want to rollback any dml done ( not necessary in your case, though )
      raise; -- I raised because of other error
end;
范例

SQL> create table x ( deptno number not null , c1 varchar2(20) , c2 varchar2(20) ) ;

Table created.

SQL> insert into x values ( 1 , 'XX' , 'YY' ) ;

1 row created.

SQL> commit ;

Commit complete.

SQL> declare
n_null exception;
pragma exception_init(n_null,-1400);
v_depto x.deptno%type;
begin
    insert into x values(NULL,'LOGISTICS','SINGAPORE');
    commit;
    exception
    when n_null then 
       -- select max value
       select max(deptno)+1 into v_depto from x;
       insert into x values(v_depto,'LOGISTICS','SINGAPORE');   
       commit;
       null; -- if I don't want an error, I set null;
    when others then 
      -- dbms_output.put_line(sqlerrm);
      rollback; -- In case you want to rollback any dml done ( not necessary in your case, though )
      raise; -- I raised because of other error
end; 
/

PL/SQL procedure successfully completed.

SQL> select * from x ;

    DEPTNO C1                   C2
---------- -------------------- --------------------
         1 XX                   YY
         2 LOGISTICS            SINGAPORE

SQL>

问题是?什么??你可以看到上面的问题。那是一个作业。您编写了代码,并在没有进一步解释的情况下发布了它。你运行代码了吗?你得到了什么?你收到错误消息了吗?下面的答案解释了你的帖子,但你没有问一个实际的问题。