Search code examples
plsql

How to make enum types in PL/SQL?


For example I want to make my own Boolean type and call it Bool. How do I do that?

Or a type for traffic lights, i.e. that has only Red, Yellow, Green in it (and null of course).


Solution

  • The closest think I could think of is:

    create or replace type lights as object
    (
      red varchar2(8),
      yellow varchar2(8),
      green varchar2(8),
    constructor function lights return self as result
    )
    

    and the body:

    create or replace type body lights is
    constructor function lights return self as result is
    begin
      self.red = 'red';
      self.yellow = 'yellow';
      self.green = 'green';
      return;
    end;
    end;
    

    Then in the code you can use it:

    declare
    l lights := new lights;
    begin
       dbms_output.put_line(l.red);
    end;