超过了递归SQL级别的最大数量(50) [英] maximum number of recursive SQL levels (50) exceeded

查看:202
本文介绍了超过了递归SQL级别的最大数量(50)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建主键而不使用序列,但是我不介意使用触发器. 我正在使用oracle 11gR2. 我的脚本包含以下内容. 一种.创建表(dept2) b.插入样本数据 C.创建触发器(查找最大值,加1,插入) d.插入另一条记录(触发器应触发) 它给出了以上错误. 下面是我的脚本.

I would like to create primary key without using sequence but I don't mind using trigger. I am using oracle 11gR2. my script contains the following. a. create table (dept2) b. insert sample data c. create trigger (find max value, add 1, insert) d. insert another record (trigger should fire) it gives the above error. below is my script.

drop table dept2 ;
create table dept2
(
deptno number(2),
dname varchar2(20),
mgr number(3)
) ;

insert into dept2 values (11, 'a', 23) ;
insert into dept2 values (12, 'b', 24) ;

select * from dept2 ;

CREATE OR REPLACE TRIGGER trig_deptno2
BEFORE INSERT ON dept2
FOR EACH ROW
DECLARE v_deptno NUMBER(2) ;
BEGIN
    SELECT MAX(deptno) INTO v_deptno
    FROM dept2 ;
    INSERT INTO dept2 VALUES
    ((v_deptno + 1), :NEW.dname, :NEW.mgr) ;
END trig_deptno2 ;
/
SHOW ERR
L

insert into dept2 (dname, mgr) values ('d', 24) ;
select * from dept2 ;
L


谁能告诉我错误在哪里或如何纠正错误?


Can anyone tell me where is the mistake or how I can correct the error?

推荐答案

为什么要这么做?

如果您需要自动生成的序列号,请使用一个序列.

If you need auto-generated sequence numbers, use a sequence.

SELECT MAX(id) + 1的两个主要问题:

  1. 不适用于并发事务

  1. It does not work with concurrent transactions

当您删除当前的"max"时,它会重复数字

It repeats numbers when you delete the current "max"

这篇关于超过了递归SQL级别的最大数量(50)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆