Trivadis / plsql-and-sql-coding-guidelines

Trivadis PL/SQL & SQL Coding Guidelines

Home Page:https://trivadis.github.io/plsql-and-sql-coding-guidelines/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

G-2130: Extend guideline regarding Oracle Database 23c SQL DOMAIN feature

PhilippSalvisberg opened this issue · comments

As mentioned in #184, the guideline might be too strict. However, introducing a threshold parameter similar to the case mentioned in #180 does not make sense IMO.

Data types can be referenced using %TYPE. However, when there is no persistent representation then %TYPE cannot be used. Of course there is probably a table/view with a similar suited data type, but the relation would be wrong.

So the better option is really to define a subtype. But where? In a centralized PL/SQL package. The problem with centralized packages is, that they the code using them is invalidated. This is especially bad in shared environments. A better option seems to be the new SQL DOMAIN feature which is planned for Oracle Database 23c. See this tweet.

The guideline G-2130 should be extended once SQL Domains become available in the Oracle Database.

I run the following in a Oracle Database 23c FREE edition:

-- example in https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/create-domain.html
create domain if not exists day_of_week as varchar2(3 char)
   constraint day_of_week_c
      check (upper(value) in ('MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'))
   deferrable initially deferred
   display substr(value, 1, 2);

-- usage in table (works)
create table t (c1 day_of_week);

-- usage in PL/SQL (does not work)
create or replace procedure p(in_p1 in day_of_week) is
begin
   sys.dbms_output.put_line(in_p1);
end;
/

The domain can be used in the table to define the type of a column. However, it cannot be used in PL/SQL to define the datatype of parameter, variable, constant. The error I've got in the example above was:

Procedure P compiled

LINE/COL  ERROR
--------- -------------------------------------------------------------
0/0       PL/SQL: Compilation unit analysis terminated
1/22      PLS-00905: object PLSCOPE.DAY_OF_WEEK is invalid
Errors: check compiler log

As a result, it's currently not possible to implement this enhancement.

Closing issue. Reopen in case a future version of the Oracle Database supports SQL Domains in PL/SQL.