我必须写一个触发器。它编译了所有内容,但如果我想在我的视图中插入一些内容,我会收到一条错误消息。也许你可以帮助我。
SET DEFINE off;
CREATE OR REPLACE TRIGGER LieferantOV_trig
INSTEAD OF INSERT
ON LIEFERANT_OV
FOR EACH ROW
BEGIN
IF INSERTING THEN
INSERT INTO Lieferant (LiefNr, Name, Adresse)
VALUES(:new.LiefNr, :new.Name, ntTAdresse());
INSERT INTO TABLE (SELECT Adresse FROM Lieferant ) VALUES
(TAdresse(:new.Straße, :new.PLZ, :new.Ort));
END IF;
END;
INSERT INTO Lieferant_OV
VALUES(752443, 'Laepple Teublitz', 'Maxstr. 12', '93158', 'Teublitz');
对于嵌套表
CREATE OR REPLACE TYPE TAdresse AS OBJECT(
Straße VARCHAR2(50),
PLZ VARCHAR2(5),
Ort VARCHAR2(50)
);
CREATE TABLE Lieferant(
LiefNr number(6) PRIMARY KEY,
Name varchar2(20) NOT NULL
);
1.
CREATE OR REPLACE TYPE ntTAdresse AS TABLE OF TAdresse;
2.
ALTER TABLE Lieferant ADD Adresse ntTAdresse NESTED TABLE Adresse STORE AS TAdresseNT;
CREATE OR REPLACE VIEW Lieferant_OV (LiefNr, Name, Straße, PLZ, ORT)
AS SELECT k.LiefNr, k.Name, l.Straße, l.PLZ, l.Ort
FROM Lieferant k, table(k.Adresse) l;
答案 0 :(得分:1)
语法应如下所示:
CREATE OR REPLACE TRIGGER LieferantOV_trig
INSTEAD OF INSERT
ON LIEFERANT_OV
FOR EACH ROW
BEGIN
INSERT INTO Lieferant (LiefNr, Name, Adresse)
VALUES(:new.LiefNr, :new.Name, ntTAdresse(TAdresse(:new.Straße,:new.PLZ,:new.ORT));
END;
您可以跳过IF INSERTING THEN
,因为您的触发器仅在INSERT
注意,每个记录最多只有一个地址,因此嵌套表没有多大意义。
为了向现有的Lieferant添加地址,您可以执行以下操作:
CREATE OR REPLACE TRIGGER LieferantOV_trig
INSTEAD OF INSERT
ON LIEFERANT_OV
FOR EACH ROW
DECLARE
lieferantCount INTEGER;
BEGIN
select count(*)
into lieferantCount
from Lieferant
where LiefNr = :new.LiefNr
and Name = :new.Name;
if lieferantCount = 0 then
INSERT INTO Lieferant (LiefNr, Name, Adresse)
VALUES(:new.LiefNr, :new.Name, ntTAdresse(TAdresse(:new.Straße,:new.PLZ,:new.ORT)));
else
UPDATE Lieferant
SET Adresse = Adresse MULTISET UNION ntTAdresse(TAdresse(:new.Straße,:new.PLZ,:new.ORT))
WHERE LiefNr = :new.LiefNr
and Name = :new.Name;
end if;
END;
答案 1 :(得分:0)
如果你想每次插入一行,我想你想要这样做。
CREATE OR REPLACE TRIGGER LieferantOV_trig
INSTEAD OF INSERT
ON LIEFERANT_OV
FOR EACH ROW
BEGIN
INSERT INTO Lieferant (LiefNr, Name, Adresse)
VALUES(:new.LiefNr, :new.Name, ntTAdresse(TAdresse(:new.Straße, :new.PLZ, :new.Ort)));
END;
/
如果Adresse列只包含一个地址,则此方法有效。但这不是嵌套表的目的。因此,您可能希望检查表中是否存在给定LiefNr的行。如果是,则仅插入嵌套表。