Skip to Main Content
  • Questions
  • Problem with PL/SQL, Cursor and trigger

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Christian.

Asked: December 06, 2016 - 7:07 pm UTC

Last updated: August 05, 2019 - 9:18 am UTC

Version: SQL Developer 4.15

Viewed 1000+ times

You Asked

Hi, I have some little problem with my exercises: i have these two tables
CREATE TABLE PRENOTAZIONI (
CodPrenotazioni NUMBER(5) PRIMARY KEY,
DataPrenotazioni DATE DEFAULT SYSDATE NOT NULL,
CodProg NUMBER (3) NOT NULL,
CodUtente VARCHAR2 (20) NOT NULL
)
STORAGE(INITIAL 128M NEXT 64M MINEXTENTS 1 MAXEXTENTS 5 PCTINCREASE 0);

CREATE TABLE POSTI(
CodPosto NUMBER (3) PRIMARY KEY,
Fila VARCHAR2 (1) CHECK (Fila='A' OR Fila='B' OR Fila='C' OR Fila='D' OR Fila='E' OR Fila='F' OR Fila='G' OR Fila='H' OR Fila='I' OR Fila='L' OR Fila='M' OR Fila='M' OR Fila='N' OR Fila='O' OR Fila='P' OR Fila='Q' OR Fila='R' OR Fila='S' OR Fila='T' OR Fila='U' OR Fila='V' OR Fila='Z') NOT NULL,
Numero NUMBER (2) NOT NULL,
CodSala NUMBER (2) NOT NULL,
CodPren NUMBER (4) NOT NULL
)
STORAGE(INITIAL 128K NEXT 64K MINEXTENTS 1 MAXEXTENTS 5 PCTINCREASE 0);

In the table POSTI i have the PK codPosto and other values that can be the same and this is the problem: i want to have only different row. For example:
INSERT INTO POSTI VALUES('1','A','1','01','000');
INSERT INTO POSTI VALUES('2','A','1','01','000');
For me is an error, because the insert for one place must be unique; to resolve this problem i have thinked a trigger, this trigger:

create or replace TRIGGER POSTI
BEFORE INSERT ON POSTI
FOR EACH ROW
DECLARE
CURSOR RAG_CURSOR IS
SELECT CODPOSTO,FILA,NUMERO,CODSALA,CODPREN FROM POSTI;
VAL_RAG RAG_CURSOR %ROWTYPE;
BEGIN
FOR VAL_RAG IN RAG_CURSOR
LOOP
IF((:NEW.FILA=VAL_RAG.FILA)AND(:NEW.NUMERO=VAL_RAG.NUMERO)AND(:NEW.CODSALA=VAL_RAG.CODSALA))
THEN DBMS_OUTPUT.PUT_LINE('Tupla già inserita');
END IF;
END LOOP;
END;

The trigger compile, but if i try to enter two rows egual with two different key, the trigger doesn't do its work.
Can you help me?

and Connor said...

You dont want a trigger for that, you want a unique constraint

SQL> CREATE TABLE POSTI(
  2  CodPosto NUMBER (3) PRIMARY KEY,
  3  Fila VARCHAR2 (1) CHECK (Fila='A' OR Fila='B' OR Fila='C' OR Fila='D' OR Fila='E' OR Fila='F' OR Fila='G' OR Fila='H' OR Fila='I' OR Fila='L' OR Fila='M' OR Fila='M' OR Fila='N' OR Fila='O' OR Fila='P' OR Fila='Q' OR Fila='R' OR Fila='S' OR Fila='T' OR Fila='U' OR Fila='V' OR Fila='Z') NOT NULL,
  4  Numero NUMBER (2) NOT NULL,
  5  CodSala NUMBER (2) NOT NULL,
  6  CodPren NUMBER (4) NOT NULL
  7  )
  8  /

Table created.

SQL>
SQL> INSERT INTO POSTI VALUES('1','A','1','01','000');

1 row created.

SQL> INSERT INTO POSTI VALUES('2','A','1','01','000');

1 row created.

SQL>
SQL> delete from posti;

2 rows deleted.

SQL>
SQL> alter table posti add constraint posti_uk unique ( Fila, Numero, CodSala, CodPren );

Table altered.

SQL>
SQL> INSERT INTO POSTI VALUES('1','A','1','01','000');

1 row created.

SQL> INSERT INTO POSTI VALUES('2','A','1','01','000');
INSERT INTO POSTI VALUES('2','A','1','01','000')
*
ERROR at line 1:
ORA-00001: unique constraint (MCDONAC.POSTI_UK) violated


SQL>




Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

trigger not found

khaled zerimeche, August 04, 2019 - 2:22 pm UTC

Hi,
thanks for your response, but please note that the script not work correctly, when i run the script i have the error: Error :ORA-20001: APP-SQLAP-10000: L'erreur ORA-04091: la table AP.AP_SUPPLIER_SITES_ALL est en mutation ; le déclencheur ou la fonction ne peut la voir ORA-06512: à "APPS.POSTI", ligne 3 ORA-06512: à "APPS.POSTI", ligne 6 ORA-04088

please see the error.
Connor McDonald
August 05, 2019 - 9:18 am UTC

My script has *no* trigger.

Remove your trigger

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library