可以解决这个问题吗?

时间:2013-10-25 08:32:19

标签: oracle oracle10g

create table account_type
(
acct_type number(3) primary key,
acct_desc Varchar2(30) not null CHECK (acct_desc IN('savings','salary','current','credit')),
acct_wd_limit number(10)
);

create sequence acct_seq;

CREATE OR REPLACE TRIGGER acct_pk
BEFORE INSERT ON account_type 
FOR EACH ROW
WHEN (new.acct_type IS NULL)
BEGIN
  SELECT acct_seq.NEXTVAL
  INTO   :new.acct_type
  FROM   account_type;
END;

我在插入account_type之前在行上收到错误。不明白为什么

我也尝试过做

CREATE OR REPLACE TRIGGER acct_pk
BEFORE INSERT ON account_type 
FOR EACH ROW
WHEN (new.id IS NULL)
BEGIN
  SELECT acct_seq.NEXTVAL
  INTO   :new.id 
  FROM   account_type;
END;

即使这样做也会给我一个错误

create sequence acct_pk
start with 1
increment by 1
max value 999
min value 1
no cycle;

谢谢

1 个答案:

答案 0 :(得分:0)

由于作者使用标记了问题,因此她无法在PL / SQL中使用sequence_name.nextVal。解决方案:

CREATE OR REPLACE TRIGGER acct_pk
  BEFORE INSERT ON account_type 
  FOR EACH ROW
DECLARE
  v_acct_type NUMBER;
BEGIN
  IF :new.acct_type IS NULL THEN
    SELECT acct_seq.NEXTVAL
      INTO   v_acct_type
    FROM   dual;
    :new.acct_type := v_acct_type;
  END IF;
END;