Florian Chrometz
Florian Chrometz

Reputation: 445

PL/SQL How do I check variable type

I need help with my short PL/SQL: I have a function done with "create of replace" it takes 1 variable and with my exception i want to check if its the right variabletype:

create or replace FUNCTION MYFunction(p_variable in BINARY_INTEGER) RETURN DATE PARALLEL_ENABLE IS

not_number EXCEPTION;

BEGIN
  IF p_variable NOT BINARY_INTEGER
  THEN RAISE not_number
END IF;

EXCEPTION
...
END

This is just the simplified version of my function. So as you can see I want to check if the given variable has the right type.

Or is there another oracle based exception that already catches this case?

Thanks for your help!

Upvotes: 1

Views: 5822

Answers (1)

Tiago Jorge
Tiago Jorge

Reputation: 23

How about overloading a function that will receive all possible (anticipated) types and return a string describing the corresponding type?

Something like this:

function f_variable_type (p_variable in BINARY_INTEGER) return varchar2
is
(
  return 'BINARY_INTEGER';

);

function f_variable_type(p_variable in varchar2) return varchar2
is
(
  return 'VARCHAR2';
);

function f_variable_type(p_variable in number) return varchar2
is
(
  return 'NUMBER';
);

(so on and so forth for all variable types)

and then use the function on your if statement:

BEGIN
  IF f_variable_type(p_variable) !='BINARY_INTEGER'
  THEN RAISE not_number
END IF;

Upvotes: 1

Related Questions