SQL触发器问题

发布于 2024-09-03 20:32:48 字数 538 浏览 1 评论 0原文

嘿伙计们,我无法让这个触发器工作,我花了一个小时左右的时间,但无法弄清楚我哪里出了问题,

CREATE OR REPLACE TRIGGER allergy

 BEFORE INSERT ON   

 DECLARE 
 med VARCHAR2(20);

 BEGIN

 SELECT  v.medication RCD.specify
 INTO  med
 FROM  visit v, relcondetails RCD
 WHERE :new.medication = v.medication AND RCD.specifiy = 'allergies';

 IF med = allergies THEN
  RAISE_APPLICATION_ERROR(-20000, 'Patient Is alergic to this medication');
 END IF;
END allergy;

当放入甲骨文时

,任何帮助将不胜感激

第 6 行出现错误:ORA-04079:无效 触发规范

Hey guys i cant get this trigger to work, ive worked on it for an hour or so and cant see to figure out where im going wrong, any help would be appreciated

CREATE OR REPLACE TRIGGER allergy

 BEFORE INSERT ON   

 DECLARE 
 med VARCHAR2(20);

 BEGIN

 SELECT  v.medication RCD.specify
 INTO  med
 FROM  visit v, relcondetails RCD
 WHERE :new.medication = v.medication AND RCD.specifiy = 'allergies';

 IF med = allergies THEN
  RAISE_APPLICATION_ERROR(-20000, 'Patient Is alergic to this medication');
 END IF;
END allergy;

When put into oracle

ERROR at line 6: ORA-04079: invalid
trigger specification

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

国粹 2024-09-10 20:32:48
CREATE OR REPLACE TRIGGER allergy BEFORE INSERT ON   

此处的表名称

FOR EACH ROW -- forgot this too

DECLARE 
    med VARCHAR2(20);

您确实应该将其声明为 %type。

    med visit.medication%type;

 BEGIN

 SELECT  v.medication RCD.specify

列之间需要逗号

    INTO  med

两列需要两个变量

    FROM  visit v, relcondetails RCD
    WHERE :new.medication = v.medication AND RCD.specifiy = 'allergies';

您的两个表之间没有连接条件,这非常糟糕。此查询将在两个表之间执行笛卡尔运算,然后返回各自列中包含“过敏”和 :new.medication 的所有表。

您可能还需要一个过滤条件来限制对特定患者或特定就诊的查询。此查询将为所有患者及其所有就诊次数进行平方。

  IF med = allergies THEN

我不知道这个 IF 中的/过敏/是什么。没有这样定义的变量,并且没有引号它不是字符串。

    RAISE_APPLICATION_ERROR(-20000, 'Patient Is alergic to this medication');

此错误消息强化了我所说的有关您的查询的内容。您认为您正在查询单个患者,但事实并非如此。

  END IF;
END allergy;

说真的,如果您正在编写软件来拯救一个人免于获得可能危及生命的药物,那么请考虑其他一些工作。我发誓我并不是说这很粗鲁,但是您的代码示例显示几乎不了解 pl/sql 语言或 sql 或任何编程背景。我认为您从一些示例代码开始,并尝试将其修改为某些内容。但你真的只剩下胡言乱语了。我开始认为这是家庭作业。

CREATE OR REPLACE TRIGGER allergy BEFORE INSERT ON   

name of table here

FOR EACH ROW -- forgot this too

DECLARE 
    med VARCHAR2(20);

You should really be declaring this as %type.

    med visit.medication%type;

 BEGIN

 SELECT  v.medication RCD.specify

Requires a comma between columns

    INTO  med

Two columns need two variables

    FROM  visit v, relcondetails RCD
    WHERE :new.medication = v.medication AND RCD.specifiy = 'allergies';

You have no join condition between your two tables, that's very bad. This query will perform a Cartesian between the two tables and then return all of them that have 'allergies' and :new.medication in their respective columns.

you also probably need a filter condition to limit the query to a particular patient or a particular visit. This query will do it for all patients and all their visits squared.

  IF med = allergies THEN

I don't know what /allergies/ is in this IF. There's no variable that's defined as that and without quotes it's not a string.

    RAISE_APPLICATION_ERROR(-20000, 'Patient Is alergic to this medication');

This error message reinforces what I said about your query. You think you're querying for a single patient but you're not.

  END IF;
END allergy;

Seriously, if you're writing software to save a person from getting potentially life threatening medication then please consider some other line of work. I swear I'm not saying this to be rude, but your code sample shows almost no understanding of the pl/sql language or sql or any scrap of programming background. I think you started with some sample code and tried to modify it into something something. But you're really left with gibberish. I'm starting to think this is homework.

凡尘雨 2024-09-10 20:32:48

除了马克指出您缺少表名称以及马丁指出您希望这是一个行级触发器之外,您的实际主体将无法编译,原因有几个。

  • 您看起来像是在尝试选择两列,但它们之间没有逗号,并且 INTO 子句中只有一个局部变量
  • 您使用了未声明的标识符 allergies任何地方。

我也怀疑你的查询在逻辑上是否正确,但当然我不知道数据库设计,所以我不能肯定地说。

In addition to Mark's point that you are missing the table name, and Martin's point that you want this to be a row-level trigger, your actual body won't compile, for a couple of reasons.

  • You look like you're trying to select two columns, but you don't have a comma between them, and you only have one local variable in the INTO clause
  • You use an identifier allergies which is not declared anywhere.

I also doubt that your query is logically correct, but of course I don't know the database design so I can't say for sure.

北方的韩爷 2024-09-10 20:32:48
BEFORE INSERT ON <TABLE NAME>

当您只选择一个变量时,为什么要同时选择 v.medication 和 RCD.specify?

BEFORE INSERT ON <TABLE NAME>

and why select both v.medication and RCD.specify when you're only selecting into one variable?

横笛休吹塞上声 2024-09-10 20:32:48

您可能已经看过这个但是:
http://msdn.microsoft.com/en-us /library/aa258254(SQL.80).aspx

CREATE TRIGGER TriggerName
ON MyTableName
FOR MyEvent
AS
     -- My Trigger Logic

You've probably seen this but:
http://msdn.microsoft.com/en-us/library/aa258254(SQL.80).aspx

CREATE TRIGGER TriggerName
ON MyTableName
FOR MyEvent
AS
     -- My Trigger Logic
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文