Skip to main content

Read data in Data Area from SQL - IBM i

Data Area (DTAARA)

Data areas have been very much useful for sharing the data with in the job and across multiple jobs on IBM i. 

There have been many ways for reading the data from Data Area based on the place it is being read (like CL, RPGLE, SQL, Command Line etc...)

In this post, we will see how to read data in Data Area from SQL using function DATA_AREA_INFO. Results returned by this function are similar to the values returned by RTVDTAARA (Retrieve Data Area) CL command and QWCRDTAA (Retrieve Data Area) API.

Let's have a look at how DATA_AREA_INFO works with an example. 

DATA_AREA_INFO

In the above query,
  • 'DATA_AREA_NAME' is the only parameter we are passing and is the only mandatory parameter. Data area passed will be considered from the library list. If not present in library list and error will be thrown. This parameter accepts some special values like,
    • *LDA - Local Data Area
    • *GDA - Group Data Area
    • *PDA - Program Initialization Parameter Data Area
DATA_AREA_INFO

In the above result, 
  • Column 'DATA_AREA_LIBRARY' returns the library in which data area is present in.
  • Column 'DATA_AREA_NAME' returns the name of the data area. 
  • Column 'DATA_AREA_TYPE' returns the type of data stored in the data area (*CHAR, *DEC or *LGL).
  • Column 'LENGTH' returns the length of the data area. 
  • Column 'DECIMAL_POSITIONS' returns the number of decimal positions (this is only valid for Decimal data areas).
  • Column 'DATA_AREA_VALUE' returns the data stored in the data area. 
  • One other column which isn't in the above result is 'DATA_AREA_BINARY_VALUE', this returns the data stored in the data area as a binary value.
In the above query, we are just passing Data Area name and the data area present in the library list would be retuned by default. We can pass the library name explicitly to read the data from the data area in specific library. 

DATA_AREA_INFO

In the above example, we are passing library name and using Decimal data area. This should now return the number of decimal positions.

DATA_AREA_INFO

One other parameter this function accepts is 'IGNORE_ERRORS'. This parameter accepts two values.
  • 'YES' - Error is ignored and the partial data is returned. Only Data Area name (and Library Name if specified) and other columns would be null. 
  • 'NO' - An error is returned. This the default value. 
Above function would only work with one data area, If we need to retrieve the same data for multiple data areas at once from SQL, 'DATA_AREA_INFO' view would be useful. 

DATA_AREA_INFO view

This query would return the data from all the data areas present in the library passed. 

DATA_AREA_INFO view

Columns returned are almost same DATA_AREA_INFO function and below are the only additional columns in this view. 
  • 'SQL_SEQUENCE' - 'YES' if the data area is defined as SQL Sequence and 'NO' if not.
  • 'TEXT_DESCRIPTION' - Description of the data area.

If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Comments

Popular posts from this blog

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short form. Below i