Giter Club home page Giter Club logo

Comments (2)

PhilippSalvisberg avatar PhilippSalvisberg commented on May 23, 2024 1

I ran the following test in a 19c Database instance:

REATE OR REPLACE TRIGGER emp_trg
  BEFORE UPDATE OF ename OR UPDATE OF job 
  ON emp
  FOR EACH ROW
BEGIN
  :new.sal := :old.sal*2;
END;
/

set echo on
select * from emp where empno = 7788;
update emp set ename = 'hallo' where empno = 7788;
select * from emp where empno = 7788;
rollback;
select * from emp where empno = 7788;
update emp set job = 'hallo' where empno = 7788;
select * from emp where empno = 7788;
rollback;
select * from emp where empno = 7788;

the result (console output) was:

SQL> set echo on
SQL> select * from emp where empno = 7788;

     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7788 SCOTT      ANALYST         7566 19.04.1987 00:00:00       3000                    20

SQL> update emp set ename = 'hallo' where empno = 7788;

1 row updated.

SQL> select * from emp where empno = 7788;

     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7788 hallo      ANALYST         7566 19.04.1987 00:00:00       3000                    20

SQL> rollback;

Rollback complete.

SQL> select * from emp where empno = 7788;

     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7788 SCOTT      ANALYST         7566 19.04.1987 00:00:00       3000                    20

SQL> update emp set job = 'hallo' where empno = 7788;

1 row updated.

SQL> select * from emp where empno = 7788;

     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7788 SCOTT      hallo           7566 19.04.1987 00:00:00       6000                    20

SQL> rollback;

Rollback complete.

SQL> select * from emp where empno = 7788;

     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7788 SCOTT      ANALYST         7566 19.04.1987 00:00:00       3000                    20

Hence, this rule makes really sense.

from plsql-and-sql-coding-guidelines.

PhilippSalvisberg avatar PhilippSalvisberg commented on May 23, 2024

from SonarSource:

The DML events clause of a trigger is not meant to be used with multiple OF conditions. When it is, only the last one will actually be taken into account, without any error message being produced. This can lead to counter-intuitive code.

Only the UPDATE event should have an OF condition, and there should be at most one occurence of it.

Noncompliant Code Example

CREATE OR REPLACE TRIGGER myTrigger
  BEFORE UPDATE OF firstName OR UPDATE OF lastName -- Noncompliant - will *only* be triggered on updates of lastName!
  ON myTable
  FOR EACH ROW
BEGIN
  NULL;
END;
/

Compliant Solution

CREATE OR REPLACE TRIGGER myTrigger
  BEFORE UPDATE OF firstName, lastName             -- Compliant - triggered on updates of firstName or/and lastName
  ON myTable
  FOR EACH ROW
BEGIN
  NULL;
END;
/

from plsql-and-sql-coding-guidelines.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.