Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make enum types in PL/SQL?

Tags:

plsql

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).

like image 751
Yrogirg Avatar asked Dec 14 '11 06:12

Yrogirg


People also ask

What is enum data type in Oracle?

An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time. See Section 11.3. 1, “String Data Type Syntax” for ENUM type syntax and length limits.

How do you declare an enum in SQL?

ENUM values are sorted based on their index numbers, which depend on the order in which the enumeration members were listed in the column specification. For example, 'b' sorts before 'a' for ENUM('b', 'a') . The empty string sorts before nonempty strings, and NULL values sort before all other enumeration values.

Do enums have types?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Is enum is a data type?

An enumeration is a data type that consists of a set of named values that represent integral constants, known as enumeration constants. An enumeration is also referred to as an enumerated type because you must list (enumerate) each of the values in creating a name for each of them.


3 Answers

I don't think that solution, provided by A.B.Cade is totally correct. Let's assume procedure like this:

procedure TestEnum(enum_in lights);

What is the value of enum_in? red? yellow? green?

I propose another solution. Here is package example

CREATE OR REPLACE PACKAGE pkg_test_enum IS

  SUBTYPE TLight IS BINARY_INTEGER RANGE 0..2;
  Red CONSTANT TLight := 0;
  Yellow CONSTANT TLight := 1;
  Green CONSTANT TLight := 2;

  --get sting name for my "enum" type
  FUNCTION GetLightValueName(enum_in TLight) RETURN VARCHAR2;

  PROCEDURE EnumTest(enum_in TLight);

END pkg_test_enum;


CREATE OR REPLACE PACKAGE BODY pkg_test_enum IS 
  FUNCTION GetLightValueName(enum_in TLight) 
  RETURN VARCHAR2
  IS
    ResultValue VARCHAR2(6);
  BEGIN
    CASE enum_in 
      WHEN Red THEN ResultValue := 'Red';
      WHEN Green THEN ResultValue := 'Green';
      WHEN Yellow THEN ResultValue := 'Yellow';
      ELSE ResultValue := '';
    END CASE;
    RETURN ResultValue;
  END GetLightValueName;


  PROCEDURE EnumTest(enum_in TLight)
  IS
  BEGIN
    --do stuff
    NULL;
  END EnumTest;
END pkg_test_enum;

I can now use TLight in different packages. I can now test enum_in against predefined values or null.

Here is usage example

begin
  pkg_test_enum.EnumTest(pkg_test_enum.Red);
end;

Besides, you can make this type not nullable.

SUBTYPE TLight IS BINARY_INTEGER RANGE 0..2 NOT NULL;

like image 74
MyDogTom Avatar answered Jan 01 '23 23:01

MyDogTom


This blog describes a way to do it using constant values

In addition to the constants, the blog defines a subtype for valid colors.

SQL> declare
  2  RED constant number(1):=1;
  3  GREEN constant number(1):=2;
  4  BLUE constant number(1):=3;
  5  YELLOW constant number(1):=4;
  6  --
  7  VIOLET constant number(1):=7;
  8  --
  9  subtype colors is binary_integer range 1..4;
 10  --
 11  pv_var colors;
 12  --
 13  function test_a (pv_var1 in colors) return colors
 14  is
 15  begin
 16     if(pv_var1 = YELLOW) then
 17             return(BLUE);
 18     else
 19             return(RED);
 20     end if;
 21  end;
 22  --
like image 26
Klas Lindbäck Avatar answered Jan 01 '23 22:01

Klas Lindbäck


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;
like image 33
A.B.Cade Avatar answered Jan 01 '23 21:01

A.B.Cade