Home » Developer & Programmer » Forms » to display user defined error when entering duplicate values
to display user defined error when entering duplicate values [message #125249] Fri, 24 June 2005 00:52 Go to next message
yarlagadda_01
Messages: 13
Registered: June 2005
Location: india
Junior Member
if there is a table with one of the column as eno and we are inserting a new record into the table(in forms only). when we entered eno that already exists in the table then we should get a user defined error message.
how can we handle it.
where to write the trigger and which trigger?

[Updated on: Fri, 24 June 2005 02:43]

Report message to a moderator

Re: to display user defined error when entering duplicate values [message #125255 is a reply to message #125249] Fri, 24 June 2005 01:43 Go to previous messageGo to next message
djmartin
Messages: 10181
Registered: March 2005
Location: Surges Bay TAS Australia
Senior Member
Account Moderator
You could place a 'unique key' constraint on the field in the table and this will cause the database to raise the error.

Alternatively, for the field in question create a When-Validate-Item trigger and use a cursor/select statement to see if the value already exists. If found, do a 'message' and a 'raise form-trigger-failure'.

David
Re: to display user defined error when entering duplicate values [message #125268 is a reply to message #125255] Fri, 24 June 2005 02:33 Go to previous messageGo to next message
yarlagadda_01
Messages: 13
Registered: June 2005
Location: india
Junior Member
Hello David,
Your answer is good but i need the exact code to be written under when validate item trigger.
Thank you
Re: to display user defined error when entering duplicate values [message #125272 is a reply to message #125268] Fri, 24 June 2005 02:43 Go to previous message
djmartin
Messages: 10181
Registered: March 2005
Location: Surges Bay TAS Australia
Senior Member
Account Moderator
IMHO your question is very close to unreasonable. Try reading the manual.

Go to http://www.oracle.com/technology/documentation/forms.html. Find 'Form Builder Reference'. Download it and search for 'when-validate-item'.
Quote:

The following example finds the commission plan in the COMMPLAN table, based on the current value of the commcode item in the EMPLOYEE block in the form, to verify that the code is valid. If the code in the COMMPLAN table is located, the description of the COMMPLAN is obtained and deposited in the non-database Description item. Otherwise, an error is raised.

/*
** Method 1: Using a SELECT...INTO statement, the trigger
** looks more readable but can be less efficient
** than Method 2 because for ANSI Standard
** compliance, the SELECT...INTO statement must
** return an error if more than one row is
** retrieved that matches the criteria. This
** implies PL/SQL may attempt to fetch data twice
** from the table in question to insure that there
** aren't two matching rows.
*/

BEGIN
   SELECT description
     INTO :Employee.Commplan_Desc
     FROM commplan
    WHERE commcode = :Employee.Commcode;
EXCEPTION
   WHEN No.Data_Found THEN
      Message ('Invalid Commission Plan, Use <List> for help');
      RAISE Form_Trigger_Failure;
   WHEN Too_Many_Rows THEN
      Message ('Error. Duplicate entries in COMMPLAN table!');
      RAISE Form_Trigger_Failure;
END;

/*
** Method 2: Using an Explicit Cursor looks a bit more
** daunting but is actually quite simple. The
** SELECT statement is declared as a named cursor
** in the DECLARE section and then is OPENed,
** FETCHed, and CLOSEd in the code explicitly
** (hence the name). Here we guarantee that only a
** single FETCH will be performed against the
** database.
*/

DECLARE
   noneFound   BOOLEAN;

   CURSOR cp IS
      SELECT description
        FROM commplan
       WHERE commcode = :Employee.Commcode;
BEGIN
   OPEN cp;

   FETCH cp
    INTO :Employee.Commplan_Desc;

   noneFound := cp%NOTFOUND;

   CLOSE cp;

   IF noneFound THEN
      Message ('Invalid Commission Plan, Use <List> for help');
      RAISE Form_Trigger_Failure;
   END IF;
END;


David
Previous Topic: Master-Detail
Next Topic: null statement
Goto Forum:
  


Current Time: Fri Sep 20 10:30:15 CDT 2024