简体   繁体   English

在PL / SQL触发器中使用Select语句的语法是什么?

[英]What is the syntax to use a Select statement inside a PL/SQL Trigger?

This is what I currently have: 这是我目前拥有的:

CREATE OR REPLACE TRIGGER MYTRIGGER
AFTER INSERT ON SOMETABLE
FOR EACH ROW    

DECLARE
 v_emplid varchar2(10);    

BEGIN
 SELECT
  personnum into v_emplid
 FROM PERSON
 WHERE PERSONID = :new.EMPLOYEEID;

dbms_output.put(v_emplid);

/* INSERT INTO SOMEOTHERTABLE USING v_emplid and some of the other values from the trigger table*/

END MYTRIGGER;    

DBA_ERRORS has this error: PL/SQL: ORA-00923: FROM keyword not found where expected DBA_ERRORS有此错误:PL / SQL:ORA-00923:找不到期望的FROM关键字

1) There must be something else to your example because that sure seems to work for me 1)您的示例中还必须包含其他内容,因为这肯定对我有用

SQL> create table someTable( employeeid number );

Table created.

SQL> create table person( personid number, personnum varchar2(10) );

Table created.

SQL> ed
Wrote file afiedt.buf

  1  CREATE OR REPLACE TRIGGER MYTRIGGER
  2    AFTER INSERT ON SOMETABLE
  3    FOR EACH ROW
  4  DECLARE
  5   v_emplid varchar2(10);
  6  BEGIN
  7   SELECT personnum
  8     into v_emplid
  9     FROM PERSON
 10    WHERE PERSONID = :new.EMPLOYEEID;
 11    dbms_output.put(v_emplid);
 12    /* INSERT INTO SOMEOTHERTABLE USING v_emplid and some of the other values
 from the trigger table*/
 13* END MYTRIGGER;
 14  /

Trigger created.

SQL> insert into person values( 1, '123' );

1 row created.

SQL> insert into sometable values( 1 );

1 row created.

2) You probably want to declare V_EMPLID as being of type Person.PersonNum%TYPE so that you can be certain that the data type is correct and so that if the data type of the table changes you won't need to change your code. 2)您可能希望将V_EMPLID声明为Person.PersonNum%TYPE类型,以便可以确定数据类型正确,这样,如果表的数据类型发生更改,则无需更改代码。

3) I assume that you know that your trigger cannot query or update the table on which the trigger is defined (so no queries or inserts into someTable). 3)我假设您知道触发器无法查询或更新定义了触发器的表(因此不存在查询或插入到someTable中)。

You are playing with Lava (not just fire) in your trigger. 您在触发器中玩的是熔岩(不仅仅是火)。 DBMS_OUTPUT in a trigger is really, really bad. 触发器中的DBMS_OUTPUT确实非常糟糕。 You can blow-out on a buffer overflow in your trigger and the whole transaction is shot. 您可以触发触发器中的缓冲区溢出,从而耗尽整个事务。 Good luck tracking that down. 祝你好运跟踪下来。 If you must do output-to-console like behavior, invoke an AUTONOMOUS TRANSACTION procedure that writes to a table. 如果必须执行类似输出到控制台的行为,请调用写入表的AUTOMONMOUS TRANSACTION过程。

Triggers are pretty evil. 触发器是非常邪恶的。 I used to like them, but they are too hard to remember about. 我曾经喜欢它们,但是它们很难记住。 They affect data often times leading to MUTATING data (scary and not just because Halloween is close). 它们通常会影响数据,从而导致数据静音(吓人,而不仅仅是因为万圣节临近)。

We use triggers to change the value of columns like .new:LAST_MODIFIED := sysdate and .new:LAST_MODIFIED_BY := user. 我们使用触发器来更改.new:LAST_MODIFIED:= sysdate和.new:LAST_MODIFIED_BY:= user等列的值。 That's it. 而已。

Don't ever allow a TRIGGER to prevent a transaction from completing. 永远不要让TRIGGER阻止交易完成。 Find another option. 寻找其他选择。

I would not use a select statment in a trigger ever. 我不会在触发器中使用选择语句。 Insert into the table rather than a select into. 插入表中而不是选择中。 Once the table already exists select into does not work in most databases. 该表一旦存在,则select into在大多数数据库中不起作用。

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

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