SQL错误删除时的Oracle触发器

时间:2017-03-24 21:24:02

标签: oracle triggers delete-row

我是Oracle中的第一个计时器并使用Netbean IDE 8,我试图阻止从酒店表中删除酒店,如果房间表有酒店的房间详细信息。这2个表位于2个不同的站点上,因此必须使用触发器。我尝试了下面的代码,但它抛出错误,如下面的第6,10,13,14行的SQL错误,

  

[例外,错误代码6,550,SQLState 65000] ORA-06550:第4行,   第19栏:PLS-00049:错误的绑定变量'' ORA-06550:第4行,列   30:PLS-00103:遇到符号"文件结束"当期待一个   以下内容:

表结构是

CREATE TABLE Hotel
(
  HotelID number not null,
  HotelName varchar2(100) not null,
  HotelType varchar2(10) not null,
  ConstrYear varchar2(10) null,
  Country varchar2 (100) not null,
  City varchar2 (50) not null,
  Address varchar2 (100) not null, 
  ContactNo varchar2(50) not null,
  Email varchar2(100) null,
  CONSTRAINT Hotel_pk PRIMARY KEY (HotelID)
);

和房间

CREATE TABLE Room 
(
 RoomID number not null, 
 HotelID raw(16) not null, 
 RoomNo number not null,
 RoomType varchar2(20) not null, 
 Price numeric(10,2) not null, 
 RoomDesc varchar2(255) not null, 
 CONSTRAINT Room_pk PRIMARY KEY (RoomID),
);

我做错了什么?请帮忙。

CREATE OR REPLACE TRIGGER CHECK_Room
BEFORE DELETE on Hotel
FOR each ROW
declare
  rowcount number;
begin
  SELECT COUNT(HotelID) INTO rowcount 
  from ROOM@site1
  where HotelID = :OLD.HotelID;
  if rowcount>0 
  THEN
     Raise_Application_Error (-20100, 'This Hotel has room details in Room table.');
  end if;
end;

1 个答案:

答案 0 :(得分:1)

如果您的数据库链接状况良好,以下内容应该对您有用 我建议不要使用保留字作为变量名。一个常见的约定是在变量名称前加上“v”,如本例所示 我还建议在数据库链接上限定模式名称。 THE_USERTHE_OTHER_DATABASE的占位符。请根据需要更换site1等。 首先在两个数据库中创建表:

--This database
CREATE TABLE HOTEL(HOTELID NUMBER);
--(On the other database)
CREATE TABLE ROOM(HOTELID NUMBER);
-- ... Set up database link

CREATE OR REPLACE TRIGGER CHECK_ROOM
BEFORE DELETE ON HOTEL
FOR EACH ROW
  DECLARE
    V_ROWCOUNT NUMBER;
  BEGIN
    SELECT COUNT(HOTELID)
    INTO V_ROWCOUNT
    FROM THE_USER.ROOM@THE_OTHER_DATABASE
    WHERE ROOM.HOTELID = :OLD.HOTELID;
    IF V_ROWCOUNT > 0
    THEN
      Raise_Application_Error(-20100, 'This Hotel has room details in Room table.');
    END IF;
  END;
  /

然后测试一下:

--Here
INSERT INTO HOTEL VALUES(19);
COMMIT;
--There
INSERT INTO ROOM VALUES(19);
COMMIT;

然后:

DELETE FROM HOTEL;
DELETE FROM HOTEL
            *
ERROR at line 1:
ORA-20100: This Hotel has room details in Room table.