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

谢谢

4

1 回答 1

0

由于作者用标记了这个问题,所以她不能sequence_name.nextVal在 PL/SQL 中使用。解决方案:

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;
于 2013-10-25T08:41:44.273 回答