Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Collection Functions

Updated on 2024-05-07 GMT+08:00

Collection Operators

  • =

    Parameter: nesttable

    Return value: true or false, Boolean type

    Description: Checks whether two collections are equal.

    Example:

    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2);
    gaussdb-#     b nest := nest(1,2);
    gaussdb-#     flag bool; 
    gaussdb-# begin
    gaussdb$#     flag := a = b;
    gaussdb$#     raise info '%', flag;
    gaussdbs$# end;
    gaussdb$# /
    INFO:  t
    ANONYMOUS BLOCK EXECUTE
  • <>

    Parameter: nesttable

    Return value: true or false, Boolean type

    Description: Checks whether two collections are not equal.

    Example:

    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2);
    gaussdb-#     b nest := nest(1,2);
    gaussdb-#     flag bool; 
    gaussdb-# begin
    gaussdb$#     flag := a <> b;
    gaussdb$#     raise info '%', flag;
    gaussdb$# end;
    gaussdb$# /
    INFO:  f
    ANONYMOUS BLOCK EXECUTE

MULTISET

  • MULTISET UNION [ALL | DISTINCT]

    Parameter: nesttable

    Return value: nesttable

    Description: Union of two collection variables. ALL indicates that duplicate elements are not removed, and DISTINCT indicates that duplicate elements are removed.

    Example:

    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2);
    gaussdb-#     b nest := nest(2,3);
    gaussdb-# begin
    gaussdb$#     a := a MULTISET UNION ALL b;
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {1,2,2,3}
    ANONYMOUS BLOCK EXECUTE
     
    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2);
    gaussdb-#     b nest := nest(2,3);
    gaussdb-# begin
    gaussdb$#     a := a MULTISET UNION DISTINCT b;
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {1,2,3}
    ANONYMOUS BLOCK EXECUTE
  • MULTISET EXCEPT [ALL | DISTINCT]

    Parameter: nesttable

    Return value: nesttable

    Description: Difference of two collection variables. Taking A MULTISET EXCEPT B as an example, ALL indicates that elements that are the same as those in B are removed from A. DISTINCT indicates that duplicate elements are removed from A first and then elements that are the same as those in B are removed from A.

    Example:

    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2,2);
    gaussdbs-#     b nest := nest(2,3);
    gaussdb-# begin
    gaussdb$#     a := a MULTISET EXCEPT ALL b;
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {1,2}
    ANONYMOUS BLOCK EXECUTE
     
    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2,2);
    gaussdb-#     b nest := nest(2,3);
    gaussdb-# begin
    gaussdb$#     a := a MULTISET EXCEPT DISTINCT b;
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {1}
    ANONYMOUS BLOCK EXECUTE
  • MULTISET INTERSECT [ALL | DISTINCT]

    Parameter: nesttable

    Return value: nesttable

    Description: Intersection of two collection variables. Taking A MULTISET INTERSECT B as an example, ALL indicates that all duplicate elements in A and B are obtained, and DISTINCT indicates that duplicate elements in A and B are obtained and then duplicate elements in this intersection are removed.

    Example:

    gaussdb=#  declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2,2);
    gaussdbs-#     b nest := nest(2,2,3);
    gaussdb-# begin
    gaussdb$#     a := a MULTISET INTERSECT ALL b;
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {2,2}
    ANONYMOUS BLOCK EXECUTE
     
    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2,2);
    gaussdb-#     b nest := nest(2,2,3);
    gaussdb-# begin
    gaussdb$#     a := a MULTISET INTERSECT DISTINCT b;
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {2}
    ANONYMOUS BLOCK EXECUTE

Functions of the Collection Type

NOTE:

In an inner expression, functions of the collection type cannot be called in nesting mode.

  • exists(idx)

    Parameter: idx is of the int4 or varchar type.

    Return value: true or false, Boolean type

    Description: Checks whether a valid element exists in a specified position.

    Example:

    gaussdb=# declare
    gaussdb-#     type nest is table of varchar2;
    gaussdb-#     a nest := nest('happy','?');
    gaussdb-#     flag bool;
    gaussdb-# begin
    gaussdb$#     flag := a.exists(1);
    gaussdb$#     raise info '%', flag;
    gaussdb$#     flag := a.exists(10);
    gaussdb$#     raise info '%', flag;
    gaussdb$# end;
    gaussdb$# /
    INFO:  t
    INFO:  f
    ANONYMOUS BLOCK EXECUTE
     
    gaussdb=# declare
    gaussdb-#     type nest is table of varchar2 index by varchar2;
    gaussdb-#     a nest;
    gaussdb-#     flag bool;
    gaussdb-# begin
    gaussdb$#     a('1') := 'Be';
    gaussdb$#     a('2') := 'happy';
    gaussdb$#     a('3') := '.';
    gaussdb$#     flag := a.exists('1');
    gaussdb$#     raise info '%', flag;
    gaussdb$#     flag := a.exists('ddd');
    gaussdb$#     raise info '%', flag;
    gaussdb$# end;
    gaussdb$# /
    INFO:  t
    INFO:  f
    ANONYMOUS BLOCK EXECUTE
  • extend[(count[, idx])]

    Parameters: idx and count are of the int4 type.

    Return value: No value is returned.

    Description: Extends one or count elements at the end of the nest-table variable. Only the nest-table collection type is supported. If index set element idx exists, count index elements are copied to the end of the variable.

    Example:

    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1);
    gaussdb-# begin
    gaussdb$#     raise info '%', a;
    gaussdb$#     a.extend;
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {1}
    INFO:  {1,NULL}
    ANONYMOUS BLOCK EXECUTE
     
    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1);
    gaussdb-# begin
    gaussdb$#     raise info '%', a;
    gaussdb$#     a.extend(2);
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {1}
    INFO:  {1,NULL,NULL}
    ANONYMOUS BLOCK EXECUTE
     
    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1);
    gaussdb-# begin
    gaussdb$#     raise info '%', a;
    gaussdb$#     a.extend(2,1);
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {1}
    INFO:  {1,1,1}
    ANONYMOUS BLOCK EXECUTE
  • delete[(idx1[, idx2])]

    Parameter: idx1 and idx2 are of the int4 or varchar2 type.

    Return value: No value is returned.

    Description: Deletes all elements and releases corresponding storage space in a nest-table collection (to use this collection, extend must be executed again), or deletes all elements (including index set elements) in an index-by table collection but does not release corresponding storage space.

    Example:

    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2,3,4,5);
    gaussdb-# begin
    gaussdb$#     raise info '%', a;
    gaussdb$#     a.delete;
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {1,2,3,4,5}
    INFO:  {}
    ANONYMOUS BLOCK EXECUTE
     
    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2,3,4,5);
    gaussdb-# begin
    gaussdb$#     raise info '%', a;
    gaussdb$#     a.delete(3);
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {1,2,3,4,5}
    INFO:  {1,2,4,5}
    ANONYMOUS BLOCK EXECUTE
     
    gaussdb=# declare
    gaussdb-#     type nest is table of int;
    gaussdb-#     a nest := nest(1,2,3,4,5);
    gaussdb-# begin
    gaussdb$#     raise info '%', a;
    gaussdb$#     a.delete(2,4);
    gaussdb$#     raise info '%', a;
    gaussdb$# end;
    gaussdb$# /
    INFO:  {1,2,3,4,5}
    INFO:  {1,5}
    ANONYMOUS BLOCK EXECUTE
  • trim[(n)]

    Parameter: n is of the int4 type.

    Return value: No value is returned.

    Description: Deletes one or n elements and corresponding storage space from a nest-table collection. Only the nest-table collection type is supported.

    Example:

    gaussdb=# declare
    gaussdb-# type nest is table of int;
    gaussdb-# aa nest:=nest(11,22,33,44,55);
    gaussdb-# begin
    gaussdb$# raise info 'aa:%' ,aa;
    gaussdb$# aa.trim;
    gaussdb$# raise info 'aa:%' ,aa;
    gaussdb$# aa.trim(2);
    gaussdb$# raise info 'aa:%' ,aa;
    gaussdb$# end;
    gaussdb$# /
    INFO:  aa:{11,22,33,44,55}
    INFO:  aa:{11,22,33,44}
    INFO:  aa:{11,22}
    ANONYMOUS BLOCK EXECUTE
  • count

    Parameter: none

    Return value: int type

    Description: Returns the number of valid elements in a collection.

    Example:

    gaussdb=# declare
    gaussdb-# type nest is table of int;
    gaussdb-# aa nest:=nest(11,22,33,44,55);
    gaussdb-# begin
    gaussdb$# raise info 'count:%' ,aa.count;
    gaussdb$# end;
    gaussdb$# /
    INFO:  count:5
    ANONYMOUS BLOCK EXECUTE
    
    gaussdb=# declare
    gaussdb-# type nest is table of int index by varchar;
    gaussdb-# aa nest;
    gaussdb-# begin
    gaussdb$# aa('aaa') := 111;
    gaussdb$# aa('bbb') := 222;
    gaussdb$# aa('ccc') := 333;
    gaussdb$# raise info 'count:%' ,aa.count;
    gaussdb$# end;
    gaussdb$# /
    INFO:  count:3
    ANONYMOUS BLOCK EXECUTE
  • first

    Parameter: none

    Return value: int or varchar

    Description: Returns the index of the first valid element in a collection.

    Example:

    gaussdb=# declare
    gaussdb-# type nest is table of int;
    gaussdb-# aa nest:=nest(11,22,33,44,55);
    gaussdb-# begin
    gaussdb$# raise info 'first:%' ,aa.first;
    gaussdb$# end;
    gaussdb$# /
    INFO:  first:1
    ANONYMOUS BLOCK EXECUTE
    
    gaussdb=# declare
    gaussdb-# type nest is table of int index by varchar;
    gaussdb-# aa nest;
    gaussdb-# begin
    gaussdb$# aa('aaa') := 111;
    gaussdb$# aa('bbb') := 222;
    gaussdb$# aa('ccc') := 333;
    gaussdb$# raise info 'first:%' ,aa.first;
    gaussdb$# end;
    gaussdb$# /
    INFO:  first:aaa
    ANONYMOUS BLOCK EXECUTE
  • last

    Parameter: none

    Return value: int or varchar

    Description: Returns the index of the last valid element in a collection.

    Example:

    gaussdb=# declare
    gaussdb-# type nest is table of int;
    gaussdb-# aa nest:=nest(11,22,33,44,55);
    gaussdb-# begin
    gaussdb$# raise info 'last:%' ,aa.last;
    gaussdb$# end;
    gaussdb$# /
    INFO:  last:5
    ANONYMOUS BLOCK EXECUTE
    
    gaussdb=# declare
    gaussdb-# type nest is table of int index by varchar;
    gaussdb-# aa nest;
    gaussdb-# begin
    gaussdb$# aa('aaa') := 111;
    gaussdb$# aa('bbb') := 222;
    gaussdb$# aa('ccc') := 333;
    gaussdb$# raise info 'last:%' ,aa.last;
    gaussdb$# end;
    gaussdb$# /
    INFO:  last:ccc
    ANONYMOUS BLOCK EXECUTE
  • prior(idx)

    Parameter: idx is of the int or varchar type.

    Return value: int or varchar type

    Description: Returns the index of a valid element before the current index in a collection.

    Example:

    gaussdb=# declare
    gaussdb-# type nest is table of int;
    gaussdb-# aa nest:=nest(11,22,33,44,55);
    gaussdb-# begin
    gaussdb$# raise info 'prior:%' ,aa.prior(3);
    gaussdb$# end;
    gaussdb$# /
    INFO:  prior:2
    ANONYMOUS BLOCK EXECUTE
    
    gaussdb=# declare
    gaussdb-# type nest is table of int index by varchar;
    gaussdb-# aa nest;
    gaussdb-# begin
    gaussdb$# aa('aaa') := 111;
    gaussdb$# aa('bbb') := 222;
    gaussdb$# aa('ccc') := 333;
    gaussdb$# raise info 'prior:%' ,aa.prior('bbb');
    gaussdb$# end;
    gaussdb$# /
    INFO:  prior:aaa
    ANONYMOUS BLOCK EXECUTE
  • next(idx)

    Parameter: idx is of the int or varchar type.

    Return value: int or varchar type

    Description: Returns the index of a valid element following the current index in a collection.

    Example:

    gaussdb=# declare
    gaussdb-# type nest is table of int;
    gaussdb-# aa nest:=nest(11,22,33,44,55);
    gaussdb-# begin
    gaussdb$# raise info 'next:%' ,aa.next(3);
    gaussdb$# end;
    gaussdb$# /
    INFO:  next:4
    ANONYMOUS BLOCK EXECUTE
    
    gaussdb=#  declare
    gaussdb-# type nest is table of int index by varchar;
    gaussdb-# aa nest;
    gaussdb-# begin
    gaussdb$# aa('aaa') := 111;
    gaussdb$# aa('bbb') := 222;
    gaussdb$# aa('ccc') := 333;
    gaussdb$# raise info 'next:%' ,aa.next('bbb');
    gaussdb$# end;
    gaussdb$# /
    INFO:  next:ccc
    ANONYMOUS BLOCK EXECUTE
  • limit

    Parameter: none

    Return value: null

    Description: Returns the maximum number of elements that can be stored in a nest-table collection. This function applies only to the array type. The return value is null.

    Example:

    gaussdb=# declare
    gaussdb-# type nest is table of int;
    gaussdb-# aa nest:=nest(11,22,33,44,55);
    gaussdb-# begin
    gaussdb$# raise info 'limit:%' ,aa.limit;
    gaussdb$# end;
    gaussdb$# /
    INFO:  limit:<NULL>
    ANONYMOUS BLOCK EXECUTE

Collection-related Functions

  • unnest_table (anynesttable) or unnest (anynesttable)

    Description: Returns a collection of elements in a nest-table.

    Return type: setof anyelement

    Example:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    create or replace procedure f1()
    as
        type t1 is table of int;
        v2 t1 := t1(null, 2, 3, 4, null);
        tmp int;
        cursor c1 is select * from unnest_table(v2);
    begin
    open c1;
    for i in 1 .. v2.count loop
        fetch c1 into tmp;
        if tmp is null then
            dbe_output.print_line(i || ': is null');
        else
            dbe_output.print_line(i || ': ' || tmp);
        end if;
    end loop;
    close c1;
    end;
    /
    
    gaussdb=# call f1();
    1: is null
    2: 2
    3: 3
    4: 4
    5: is null
     f1 
    ----
    
    (1 row)
    
    -- Example: nested record types in a nest-table
    create or replace procedure p1() is
      type rec is record(c1 int, c2 int);
      type t1 is table of rec;
    
      v t1 := t1(rec(1, 1), rec(2, null), rec(null, null), null);
      v2 t1 := t1();
      cursor cur is select * from unnest(v);
    begin
      v2.extend(v.count);
      open cur;
      for i in 1 .. v.count loop
        fetch cur into v2(i);
        raise info '%', v2(i);
      end loop;
      close cur;
    end;
    /
    
    gaussdb=# call p1();
    INFO:  (1,1)
    INFO:  (2,)
    INFO:  (,)
    INFO:  (,)
     p1 
    ----
    
    (1 row)
    
    NOTE:

    If the collection of elements are of record type and the elements are NULL, a record whose all columns are NULL is returned. For details, see the example.

  • unnest_table (anyindexbytable) or unnest (anyindexbytable)

    Description: Returns the collection of elements in an index-by table sorted by index.

    Return type: setof anyelement

    Constraint: Only the index-by int type is supported. The index-by varchar type is not supported.

    Example:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    create or replace procedure f1()
    as
        type t1 is table of int index by int;
        v2 t1 := t1(1=>1, -10=>(-10), 6=>6, 4=>null);
        tmp int;
        cursor c1 is select * from unnest_table(v2);
    begin
    open c1;
    for i in 1 .. v2.count loop
        fetch c1 into tmp;
        if tmp is null then
            dbe_output.print_line(i || ': is null');
        else
            dbe_output.print_line(i || ': ' || tmp);
        end if;
    end loop;
    close c1;
    end;
    /
    
    gaussdb=# call f1();
    1: -10
    2: 1
    3: is null
    4: 6
     f1 
    ----
    
    (1 row)
    
    -- Example: nested record types in an index-by table
    create or replace procedure p1() is
      type rec is record(c1 int, c2 int);
      type t1 is table of rec index by int;
    
      v t1 := t1(1 => rec(1, 1), 2 => rec(2, null), 3 => rec(null, null), 4 => null);
      v2 t1 := t1();
      cursor cur is select * from unnest(v);
    begin
      open cur;
      for i in 1 .. v.count loop
        fetch cur into v2(i);
        raise info '%', v2(i);
      end loop;
      close cur;
    end;
    /
    
    gaussdb=# call p1();
    INFO:  (1,1)
    INFO:  (2,)
    INFO:  (,)
    INFO:  (,)
     p1 
    ----
    
    (1 row)
    
    NOTE:

    If the collection of elements are of record type and the elements are NULL, a record whose all columns are NULL is returned. For details, see the example.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback