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

GET DESCRIPTOR

Function

Retrieves information about a query result set and stores it into host variables. A descriptor area is typically populated using FETCH or SELECT before using this command to transfer the information into host language variables. This command can be in either of the following formats:
  • Retrieves the descriptor "header" items, which applies to the result set in its entirety.
  • Retrieves information about a particular column, requiring the column number as additional parameter.

Syntax

GET DESCRIPTOR descriptor_name VALUE column_number :cvariable = descriptor_item [, ... ]
GET DESCRIPTOR descriptor_name:cvariable = descriptor_header_item [, ... ]

Parameters

  • descriptor_name

    Descriptor name.

  • descriptor_header_item

    Identifies which header item is to be retrieved. Currently, only COUNT that is used to obtain the number of columns in the result set is supported.

  • column_number

    Number of the column about which information is to be retrieved. The count starts at 1.

  • descriptor_item

    Identifies which information item about a column is to be retrieved.

  • cvariable

    A host variable that will receive the data retrieved from the descriptor area.

Examples

Retrieve the number of columns in a result set.
EXEC SQL GET DESCRIPTOR d :d_count = COUNT;
Retrieve the data length in the first column.
EXEC SQL GET DESCRIPTOR d VALUE 1 :d_returned_octet_length = RETURNED_OCTET_LENGTH;
Retrieve the data body of the second column as a string.
EXEC SQL GET DESCRIPTOR d VALUE 2 :d_data = DATA;
Execute SELECT current_database();. The number of columns, column data length, and column data are displayed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) 
{ 
EXEC SQL BEGIN DECLARE SECTION; 
    int  d_count; 
    char d_data[1024]; 
    int  d_returned_octet_length; 
EXEC SQL END DECLARE SECTION; 
    EXEC SQL CONNECT TO testdb AS con1 USER testuser; 
    EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT; 
    EXEC SQL ALLOCATE DESCRIPTOR d; 

    /* Declare and open a cursor, and allocate a descriptor to the cursor. */
    EXEC SQL DECLARE cur CURSOR FOR SELECT current_database(); 
    EXEC SQL OPEN cur; 
    EXEC SQL FETCH NEXT FROM cur INTO SQL DESCRIPTOR d; 
  
    /* Obtain the total number of columns. */
    EXEC SQL GET DESCRIPTOR d :d_count = COUNT; 
    printf("d_count = %d\n", d_count); 

    /* Obtain the length of a returned column. */
    EXEC SQL GET DESCRIPTOR d VALUE 1 :d_returned_octet_length = RETURNED_OCTET_LENGTH; 
    printf("d_returned_octet_length = %d\n", d_returned_octet_length); 

    /* Fetch the returned column as a string. */
    EXEC SQL GET DESCRIPTOR d VALUE 1 :d_data = DATA; 
    printf("d_data = %s\n", d_data); 

    /* Closed */ 
    EXEC SQL CLOSE cur; 
    EXEC SQL COMMIT; 
  
    EXEC SQL DEALLOCATE DESCRIPTOR d; 
    EXEC SQL DISCONNECT ALL; 
    return 0; 
}
The following is the command output.
d_count                 = 1 
d_returned_octet_length = 6 
d_data                  = testdb