The TRIM method is used to remove elements from the end of a variable-length array or nested table.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only MySQL mode.
The TRIM method is used as follows:
The
TRIMmethod removes one element from the end of a collection. Therefore, the collection must contain at least one element. Otherwise, the predefined exceptionSUBSCRIPT_BEYOND_COUNTis raised.The
TRIM(n)method removesnelements from the end of a collection. Therefore, the collection must contain at leastnelements. Otherwise, the predefined exceptionSUBSCRIPT_BEYOND_COUNTis raised.
The TRIM method operates based on the internal size of the collection. In other words, if the DELETE method removes an element but leaves a placeholder for it, the TRIM method considers the element to still exist. Therefore, the TRIM method can remove elements that have been deleted.
PL/SQL does not retain placeholders for trimmed elements. Therefore, trimmed elements are not included in the internal size of the collection, and you cannot restore trimmed elements by assigning valid values to them.
Notice
Do not rely on the interaction between TRIM and DELETE. Consider nested tables as fixed-size arrays (using only DELETE) or stacks (using only TRIM and EXTEND).
Here is an example:
obclient> DECLARE
t nested_type:= nested_type('A', 'B', 'C', 'D', 'E', 'F');
BEGIN
print_t(t);
t.TRIM; -- Trims the last element
print_t(t);
t.DELETE(3); -- Deletes the third element
print_t(t);
t.TRIM(2); -- Trims the last two elements
print_t(t);
END;
/
Query OK, 0 rows affected
t.(1) = A
t.(2) = B
t.(3) = C
t.(4) = D
t.(5) = E
t.(6) = F
---
t.(1) = A
t.(2) = B
t.(3) = C
t.(4) = D
t.(5) = E
---
t.(1) = A
t.(2) = B
t.(4) = D
t.(5) = E
---
t.(1) = A
t.(2) = B
---
In this example, a nested table variable is declared and initialized with six elements. The following operations are performed:
The last element is trimmed.
The fourth element is deleted.
The last two elements are trimmed.
The print_nt stored procedure outputs the nested table variable after initialization and after the TRIM and DELETE operations.