Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On which datatypes can I use Oracle PL/SQL RANGE clause to constrain a variable?

I'm trying to figure out when the use of the RANGE clause is allowed in variable definition within the declarative section of a PL/SQL block.

The following code was tested on Oracle 12c

This works...

declare
   l_constrained  PLS_INTEGER  RANGE  7..10 ;
begin
   l_constrained := 9;
end ;
/

This gives a compilation error...

declare
   l_constrained  NUMBER(2)  RANGE  7..10 ;
begin
   l_constrained := 9;
end ;
/

It seems to work only when applied to PL/SQL data types and not with SQL data types, but that's just my first impression.

Could anybody please give me some info and possibly head me to Oracle's official documentation covering the use of the RANGE clause? I cannot find it...

like image 397
Max Avatar asked Sep 26 '22 23:09

Max


1 Answers

Never even heard of this feature before.

Anyway, this seems to be called a "constrained subtype" and work only with PLS_INTEGER and subsets.

The only base types for which you can specify a range of values are PLS_INTEGER and its subtypes (both predefined and user-defined).

https://docs.oracle.com/database/121/LNPLS/datatypes.htm#CHDBBCIE

The documentation doesn't explicitly mention RANGE usage outside of subtype declaration, but I guess it somehow falls into the same category as declaring variable as NUMBER(8,2), i.e. declaring with a constraint on a type.

like image 83
Timekiller Avatar answered Oct 03 '22 13:10

Timekiller