选择

时间:2018-04-27 09:36:11

标签: oracle plsql triggers

我想查看BEFORE TRIGGER中的一些值,但值与我在数据库中看到的值不同。

这是我的触发器:

create or replace TRIGGER TAH_BEFORE
BEFORE INSERT ON TAH
FOR EACH ROW
DECLARE
  V_HRA_ID number := -1;
  V_OBLAST_ID number := -1;
  V_RESULT number := -1;
BEGIN

  V_RESULT := -1;
  begin
    select H.ID, H.OBLAST_ID into V_HRA_ID, V_OBLAST_ID from HRA H where H.STAV_ID  = 0;
    exception
      when no_data_found
      then raise_application_error(-20001, 'No game in playing state.', TRUE);
  end;

  begin
    select P.SHOWN into V_RESULT from POLE P where P.OBLAST_ID = V_OBLAST_ID and P.ROW_ID = :new.ROW_ID and P.COL_ID = :new.COL_ID;
    if V_RESULT > 0 then
        raise_application_error(-20002, 'Already shown.' || V_RESULT || ' ' || V_OBLAST_ID || ' ' || :new.ROW_ID || ' ' || :new.COL_ID, TRUE);
    end if;
  end;

  begin
    select count(M.ID) into V_RESULT from MINA M
    join HRA H on H.OBLAST_ID = M.OBLAST_ID
    where H.id = V_HRA_ID;
    if V_RESULT > 0 then
      raise_application_error(-20003, 'This field is marked as mine.', TRUE);
    end if;
  end;

  :new.TIMESTAMP := CURRENT_TIMESTAMP;
  :new.HRA_ID := V_HRA_ID;
END;

如果我跑

Insert into TAH(ROW_ID, COL_ID) VALUES(5,5);

我收到错误消息

Error starting at line : 2 in command -
insert into TAH (ROW_ID, COL_ID) VALUES (5, 5)
Error report -
ORA-20002: Already shown.1 143 5 5
ORA-06512: na "SK1X1.TAH_BEFORE", line 29
ORA-04088: chyba během provádění triggeru 'SK1X1.TAH_BEFORE'

但是如果我用select检查数据库(或者我在oracle sqldeveloper的GUI中检查表数据)POLE表中的所有行都有SHOWN = 0。 第一个insert intoVALUES(4,4)一起没有错误。但是,因为每个下一个(具有不同的值)给我这个错误。我在触发器中写错了吗?

修改 表格POLEHRA

的DDL
--------------------------------------------------------
--  DDL for Table POLE
--------------------------------------------------------

  CREATE TABLE "SK1X1"."POLE" 
   (    "ID" NUMBER, 
    "ROW_ID" NUMBER(3,0), 
    "COL_ID" NUMBER(3,0), 
    "OBLAST_ID" NUMBER, 
    "VALUE" NUMBER(2,0), 
    "SHOWN" NUMBER(1,0) DEFAULT 0
   ) SEGMENT CREATION IMMEDIATE 
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "STUDENTS2014" ;
--------------------------------------------------------
--  DDL for Table HRA
--------------------------------------------------------

  CREATE TABLE "SK1X1"."HRA" 
   (    "ID" NUMBER, 
    "FIRST_MOVE" TIMESTAMP (6) DEFAULT '01-01-17', 
    "LAST_MOVE" TIMESTAMP (6) DEFAULT '01-01-17', 
    "TAGGED_MINES_COUNT" NUMBER(4,0) DEFAULT 0, 
    "STAV_ID" NUMBER, 
    "OBLAST_ID" NUMBER
   ) SEGMENT CREATION IMMEDIATE 
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "STUDENTS2014" ;
--------------------------------------------------------
--  DDL for Index POLE_PK
--------------------------------------------------------

  CREATE UNIQUE INDEX "SK1X1"."POLE_PK" ON "SK1X1"."POLE" ("ID") 
  PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "STUDENTS2014" ;
--------------------------------------------------------
--  DDL for Index HRA_PK
--------------------------------------------------------

  CREATE UNIQUE INDEX "SK1X1"."HRA_PK" ON "SK1X1"."HRA" ("ID") 
  PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "STUDENTS2014" ;
--------------------------------------------------------
--  DDL for Trigger POLE_TRG
--------------------------------------------------------

  CREATE OR REPLACE TRIGGER "SK1X1"."POLE_TRG" 
BEFORE INSERT ON POLE 
FOR EACH ROW 
BEGIN
  <<COLUMN_SEQUENCES>>
  BEGIN
    IF INSERTING AND :NEW.ID IS NULL THEN
      SELECT POLE_SEQ.NEXTVAL INTO :NEW.ID FROM SYS.DUAL;
    END IF;
  END COLUMN_SEQUENCES;
END;
/
ALTER TRIGGER "SK1X1"."POLE_TRG" ENABLE;
--------------------------------------------------------
--  DDL for Trigger HRA_TRG
--------------------------------------------------------

  CREATE OR REPLACE TRIGGER "SK1X1"."HRA_TRG" 
BEFORE INSERT ON HRA 
FOR EACH ROW 
BEGIN
  <<COLUMN_SEQUENCES>>
  BEGIN
    IF INSERTING AND :NEW.ID IS NULL THEN
      SELECT HRA_SEQ1.NEXTVAL INTO :NEW.ID FROM SYS.DUAL;
    END IF;
  END COLUMN_SEQUENCES;
END;
/
ALTER TRIGGER "SK1X1"."HRA_TRG" ENABLE;
--------------------------------------------------------
--  Constraints for Table POLE
--------------------------------------------------------

  ALTER TABLE "SK1X1"."POLE" ADD CONSTRAINT "POLE_CHK1" CHECK (shown in ( '1', '0' )) ENABLE;
  ALTER TABLE "SK1X1"."POLE" ADD CONSTRAINT "POLE_PK" PRIMARY KEY ("ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "STUDENTS2014"  ENABLE;
  ALTER TABLE "SK1X1"."POLE" MODIFY ("ID" NOT NULL ENABLE);
  ALTER TABLE "SK1X1"."POLE" MODIFY ("ROW_ID" NOT NULL ENABLE);
  ALTER TABLE "SK1X1"."POLE" MODIFY ("COL_ID" NOT NULL ENABLE);
  ALTER TABLE "SK1X1"."POLE" MODIFY ("OBLAST_ID" NOT NULL ENABLE);
  ALTER TABLE "SK1X1"."POLE" MODIFY ("VALUE" NOT NULL ENABLE);
  ALTER TABLE "SK1X1"."POLE" MODIFY ("SHOWN" NOT NULL ENABLE);
--------------------------------------------------------
--  Constraints for Table HRA
--------------------------------------------------------

  ALTER TABLE "SK1X1"."HRA" ADD CONSTRAINT "HRA_PK" PRIMARY KEY ("ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "STUDENTS2014"  ENABLE;
  ALTER TABLE "SK1X1"."HRA" MODIFY ("ID" NOT NULL ENABLE);
  ALTER TABLE "SK1X1"."HRA" MODIFY ("TAGGED_MINES_COUNT" NOT NULL ENABLE);
  ALTER TABLE "SK1X1"."HRA" MODIFY ("STAV_ID" NOT NULL ENABLE);
  ALTER TABLE "SK1X1"."HRA" MODIFY ("OBLAST_ID" NOT NULL ENABLE);
  ALTER TABLE "SK1X1"."HRA" MODIFY ("FIRST_MOVE" NOT NULL ENABLE);
  ALTER TABLE "SK1X1"."HRA" MODIFY ("LAST_MOVE" NOT NULL ENABLE);

1 个答案:

答案 0 :(得分:0)

我看到的一个可能原因是当你执行第二个块的select语句时。你这么简单

select P.SHOWN into V_RESULT from...此处不确定列P.SHOWN的数据类型。可能会发生一些隐含的转换。因此,将count(P.SHOWN)带入v_rsult是件好事。

试试这个: 编辑: - 我根据您提供的信息量和我的工作正常修改了代码。见下文:

CREATE OR REPLACE TRIGGER TAH_BEFORE
BEFORE INSERT ON TAH
FOR EACH ROW
DECLARE
  V_HRA_ID number := -1;
  V_OBLAST_ID number := -1;
  V_RESULT number := -1;
BEGIN

  V_RESULT := -1;
  begin
    SELECT H.ID, H.OBLAST_ID 
    INTO V_HRA_ID, V_OBLAST_ID 
    from HRA H where H.STAV_ID  = 0;
    exception
      WHEN no_data_found
      --then raise_application_error(-20001, 'No game in playing state.', TRUE);
      then dbms_output.put_line( 'No game in playing state.');
  end;

  BEGIN
    SELECT count(P.SHOWN) 
      INTO V_RESULT 
    FROM POLE P 
    WHERE P.OBLAST_ID = V_OBLAST_ID 
    AND P.ROW_ID = :NEW.ROW_ID 
    AND P.COL_ID = :NEW.COL_ID;

    if V_RESULT > 0 then
        raise_application_error(-20002, 'Already shown.' || V_RESULT || ' ' || V_OBLAST_ID || ' ' || :new.ROW_ID || ' ' || :new.COL_ID, TRUE);
    end if;
  END;
/*
  begin
    select count(M.ID) into V_RESULT from MINA M
    join HRA H on H.OBLAST_ID = M.OBLAST_ID
    where H.id = V_HRA_ID;
    if V_RESULT > 0 then
      raise_application_error(-20003, 'This field is marked as mine.', TRUE);
    end if;
  end; */

  --:NEW.TIMESTAMP := CURRENT_TIMESTAMP;
  --:new.HRA_ID := V_HRA_ID;
END;

执行:

Insert into TAH(ROW_ID, COL_ID) VALUES(5,5);
Insert into TAH(ROW_ID, COL_ID) VALUES(4,4);

输出:

select * from tah;

Row_id  Col_id
-----   ------
5         5
4         4