Skip to content

Instantly share code, notes, and snippets.

@tschf
Last active May 7, 2017 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tschf/01c38f58886ed6fae06a435385e134c8 to your computer and use it in GitHub Desktop.
Save tschf/01c38f58886ed6fae06a435385e134c8 to your computer and use it in GitHub Desktop.
Oracle: case with a tuple (object)
create type ot_info is object (
a number,
b number,
c number,
map member function equals return raw
);
/
create type body ot_info as
map member function equals return raw as
begin
--comparison logic taken from: http://www.oratable.com/oracle-object-type-comparison/
return utl_raw.cast_to_raw(
nvl(self.a, -1)
||nvl(self.b, -1)
||nvl(self.c, -1)
);
end equals;
end;
/
create table params (
id number primary key,
a number,
b number,
c number
);
/
insert into params values (1, 1,2,3);
insert into params values (2, 3,4,5);
insert into params values (3, 1,2,4);
select
id,
a,
b,
c,
case ot_info(a,b,c)
when ot_info(1,2,3) then 1
else 0
end decoded
from params;
drop table params;
drop type ot_info;
--Script output
--
--Type OT_INFO compiled
--
--
--Type Body OT_INFO compiled
--
--
--Table PARAMS created.
--
--
--1 row inserted.
--
--
--1 row inserted.
--
--
--1 row inserted.
--
--
-- ID A B C DECODED
------------ ---------- ---------- ---------- ----------
-- 1 1 2 3 1
-- 2 3 4 5 0
-- 3 1 2 4 0
--
--
--Table PARAMS dropped.
--
--
--Type OT_INFO dropped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment