Home » SQL & PL/SQL » SQL & PL/SQL » Check for duplicate values!
Check for duplicate values! [message #39421] Tue, 16 July 2002 08:21 Go to next message
Deepa
Messages: 269
Registered: November 2000
Senior Member
I have a field called SSN!Now when the user enters a value in this field,I must validate it so that the value does not already exist in the database.If it exists,then I must give an error message,saying that "SSN already exists!".

Can anybody let me know how to do this validation???
Re: Check for duplicate values! [message #39423 is a reply to message #39421] Tue, 16 July 2002 09:21 Go to previous message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
Assuming user is trying to INSERT or UPDATE a duplicate record,
1. create a primary key on the column.
   and use execption handling to trap the eror.
2. else use a trigger.
the following may help. ( You will encounter a mutating table problem). so this is the workaround.
Standard oracle table EMP is used.
This is to check if the employee is already existing.
----------------------------------------------------

SQL> create or replace package projPak
  2      as
  3        empno number;
  4      end;
  5  /

Package created.

SQL> create or replace trigger RcheckPCount
  2      before insert or update  on emp 
  3      for each row
  4      begin
  5        projPak.empno := :new.empno;
  6  end;
  7  /

Trigger created.

SQL>     create or replace trigger ScheckPCount
  2  after insert or update on emp
  3  declare
  4  v_cnt number;
  5  begin
  6  select count(*) into v_cnt from emp
  7   where empno = projpak.empno;
  8  if v_cnt > 1  then
  9   raise_application_error(-20000,'The employee already Exists');
 10  end if;
 11  end;
 12  /

Trigger created.

SQL> INSERT INTO EMP VALUES
  2  (7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,NULL,20);
INSERT INTO EMP VALUES
            *
ERROR at line 1:
ORA-20000: The employee already Exists
ORA-06512: at "MAG.SCHECKPCOUNT", line 7
ORA-04088: error during execution of trigger 'MAG.SCHECKPCOUNT'
Previous Topic: dynamic sql for ASP access.
Next Topic: cursor for update
Goto Forum:
  


Current Time: Thu Apr 18 23:40:55 CDT 2024