简体   繁体   English

PL / SQL触发之前-从其他表继承列

[英]PL/SQL Before Trigger - Inherit column from other table

I'm very very very new to PL/SQL so excuse my ignorance on this subject, but I'd appreciate your help! 我是PL / SQL的新手,请原谅我对此问题的无知,但感谢您的帮助!

I have these 2 tables, let's say, RATINGS and GAMES. 我有这2个表格,比方说RATINGS和GAMES。

Suppose that I want to inherit a value from the RATINGS table and insert it into the GAME table automatically depending on the GAMERATING (inheriting it from the RATINGS TABLE) I gave it. 假设我想从RATINGS表继承一个值并将其自动插入GAME表中,具体取决于我给它的GAMERATING(从RATINGS表继承)。

On the GAME table we have: (GAMEID, GAMEPRICE, GAMENAME, GAMERATING, GAMEAGES, PUBLISHERID) 在GAME表格上,我们有:(GAMEID,GAMEPRICE,GAMENAME,GAMERATING,GAMEAGES,PUBLISHERID)

And on the RATINGS table we have: (RATINGID, RATINGCODE, RATINGAGES) 在RATINGS表上,我们有:(RATINGID,RATINGCODE,RATINGAGES)

If I insert the following values into GAME: (1,50,'Lost','M') I want it to automatically insert the GAMEAGES from the RATINGS table using the GAMERATING as a basis for reading from the RATINGS table, specifically from RATINGCODE. 如果我在GAME中插入以下值:(1,50,'Lost','M')我希望它使用GAMERATING作为从RATINGS表(尤其是从RATINGCODE)读取的基础,自动从RATINGS表中插入GAMEAGES 。

How would I go about on doing this? 我将如何进行呢?

All I know is I have to declare variables and insert the ages into them depending on the GAMERATING. 我所知道的是,我必须声明变量并根据GAMERATING将年龄插入其中。

EDIT: 编辑:

Managed to do it guys, 设法做到了,伙计们,

here's the format I used: 这是我使用的格式:

CREATE OR REPLACE TRIGGER [triggername] 创建或替换触发器[触发器名称]

BEFORE INSERT ON [tablename] 在[表名]上插入之前

FOR EACH ROW 每行

DECLARE 宣布

[variables] [变量]

BEGIN 开始

SELECT [columns to populate variables] INTO [variables] FROM [second table] JOIN [first table] ON [secondtableid] = :New.[firsttableid] GROUP BY [columns to pop variables] ORDER BY MAX([firsttableid]) SELECT [要填充变量的列] INTO [变量]来自[第二个表] JOIN [第一张表] ON [第二个tableid] =:新建。[firsttableid] GROUP BY [要弹出变量的列] ORDER BY MAX([firsttableid])

:New.[firsttablecolumn] := [variables] :New。[firsttablecolumn]:= [变量]

END 结束

Hope this helps someone in the future 希望这对以后的人有帮助

CREATE OR REPLACE TRIGGER [triggername]
BEFORE INSERT ON [tablename]
FOR EACH ROW
DECLARE
[variables]
BEGIN
SELECT [columns to populate variables] INTO [variables] FROM [second table] JOIN [first table] ON [secondtableid] = :New.[firsttableid] GROUP BY [columns to pop variables] ORDER BY MAX([firsttableid])
:New.[firsttablecolumn] := [variables]
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM