Here is a way to use UNNEST Table function in DB2. It takes an array of elements as input and returns output in Table format. So it is called the Table function.

UNNEST Table function

When you use this function in high-level programming languages(COBOL), ensure to pass an array of data as input to get output in Table format.

The schema for this function is SYSIBM.

Sample SQL query

The RECENT_CALLS data is an array of phone numbers. It reads this input and gives output in Table format.

SELECT T.ID, T.NUM 
FROM UNNEST(RECENT_CALLS) 
WITH ORDINALITY AS T(NUM, ID)

Output

ID NUM 
---------- 
1 9055553907 
2 4165554213 
3 4085553678

The Purpose of UNNEST

  • The function you need to use in FROM clause
  • The UNNEST function can only be used in a collection-derived-table clause in a context where arrays are supported (SQLSTATE 42887).

References

Related