The TRIM method is used to remove elements from the end of a varray or nested table.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only MySQL mode.
The usage of the TRIM method is described 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_COUNTwill be raised.The
TRIM(n)method removesnelements from the end of a collection. Therefore, the collection must contain at leastnelements; otherwise, the predefined exceptionSUBSCRIPT_BEYOND_COUNTwill be 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. Treat 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; -- Trim the last element.
print_t(t);
t.DELETE(3); -- Delete the third element.
print_t(t);
t.TRIM(2); -- Trim 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:
Trim the last element.
Delete the fourth element.
Trim the last two elements.
The print_nt stored procedure outputs the nested table variable after initialization and after the TRIM and DELETE operations.