Home » SQL & PL/SQL » SQL & PL/SQL » Can someone help me with this stored procedure
Can someone help me with this stored procedure [message #10135] Tue, 06 January 2004 03:32 Go to next message
Pieter
Messages: 6
Registered: August 2002
Junior Member
i want to make a stored procedure. This procedure must check if a day of birth is realistic. ( so it can not be greater than the current day, the sysdate)
for example when you want to insert 20/01/2004, sql must say that this is impossible!
Re: Can someone help me with this stored procedure [message #10157 is a reply to message #10135] Tue, 06 January 2004 21:44 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Here's one possibility:
SQL> create or replace procedure check_date(v_date in date)
  2  is
  3    not_realistic exception;
  4    pragma exception_init(not_realistic,-20001);
  5  begin
  6    if v_date > sysdate Then
  7       raise not_realistic;
  8    end if;
  9  exception
 10    when not_realistic then
 11      raise_application_error(-20001,'A birth date cannot be in the future');
 12  end;
 13  /

Procedure created.

SQL> exec check_date(to_date('&ddmmyyyy','ddmmyyyy'))
Enter value for ddmmyyyy: 21012004
begin check_date(to_date('21012004','ddmmyyyy')); end;

*
ERROR at line 1:
ORA-20001: A birth date cannot be in the future
ORA-06512: at "MHE.CHECK_DATE", line 11
ORA-06512: at line 1

SQL> exec check_date(to_date('&ddmmyyyy','ddmmyyyy'))
Enter value for ddmmyyyy: 05122003

PL/SQL procedure successfully completed.
I would use a trigger though:
SQL> create table persons(name varchar2(255)
  2                      ,birth_date date
  3                      );

Table created.

SQL> CREATE TRIGGER BRIU_BIRTH_DATE
  2  BEFORE  INSERT  OR UPDATE  ON PERSONS
  3  FOR EACH ROW
  4  Declare
  5    invalid_date Exception;
  6    pragma exception_init(invalid_date,-20001);
  7  Begin
  8    If :new.birth_date > trunc(sysdate) Then
  9      Raise invalid_date;
 10    End If;
 11  Exception
 12    When invalid_date Then
 13      Raise_Application_Error(-20001,'A birth date cannot be in the future!');
 14  End;
 15  /

Trigger created.
<I>-- Insert a record with a birth_date in the past</I>
SQL> insert into persons values('Mike',SYSDATE-2);
<I>-- success!</I>
1 row created.

<I>-- Insert a record with a future birth_date</I>
SQL> insert into persons values('Valerie', SYSDATE+5);
<I>-- It fails!</I>
insert into persons values('Valerie', SYSDATE+5)
            *
ERROR at line 1:
ORA-20001: A birth date cannot be in the future!
ORA-06512: at "MHE.BRIU_BIRTH_DATE", line 10
ORA-04088: error during execution of trigger 'MHE.BRIU_BIRTH_DATE'
MHE
Re: Can someone help me with this stored procedure [message #10159 is a reply to message #10157] Tue, 06 January 2004 22:38 Go to previous message
William Robertson
Messages: 1643
Registered: August 2003
Location: London, UK
Senior Member
You could also set a limit of say 120 years in the past, e.g:

IF :new.birth_date < ADD_MONTHS(SYSDATE,-1200) THEN...
Previous Topic: sql
Next Topic: Can only SQL be used to create views?
Goto Forum:
  


Current Time: Thu Apr 25 12:54:42 CDT 2024