Postgresql 中文操作指南

GET DESCRIPTOR

GET DESCRIPTOR — 从 SQL 描述符区域获取信息

GET DESCRIPTOR — get information from an SQL descriptor area

Synopsis

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

Description

GET DESCRIPTOR 从 SQL 描述符区域中检索有关查询结果集的信息,并将其存储到主机变量中。通常使用 FETCHSELECT 填充描述符区域,然后再使用此命令将信息传输到主机语言变量中。

GET DESCRIPTOR retrieves information about a query result set from an SQL descriptor area 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 has two forms: The first form retrieves descriptor “header” items, which apply to the result set in its entirety. One example is the row count. The second form, which requires the column number as additional parameter, retrieves information about a particular column. Examples are the column name and the actual column value.

Parameters

  • descriptor_name #

    • A descriptor name.

  • descriptor_header_item #

    • A token identifying which header information item to retrieve. Only COUNT, to get the number of columns in the result set, is currently supported.

  • column_number #

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

  • descriptor_item #

    • A token identifying which item of information about a column to retrieve. See Section 36.7.1 for a list of supported items.

  • cvariable #

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

Examples

一个检索结果集中列数的示例:

An example to retrieve the number of columns in a result set:

EXEC SQL GET DESCRIPTOR d :d_count = COUNT;

一个检索第一列中数据长度的示例:

An example to retrieve a data length in the first column:

EXEC SQL GET DESCRIPTOR d VALUE 1 :d_returned_octet_length = RETURNED_OCTET_LENGTH;

一个将第二列的数据主体作为字符串检索的示例:

An example to retrieve the data body of the second column as a string:

EXEC SQL GET DESCRIPTOR d VALUE 2 :d_data = DATA;

这是一个执行 SELECT current_database(); 的整个过程的示例,它显示了列数、列数据长度和列数据:

Here is an example for a whole procedure of executing SELECT current_database(); and showing the number of columns, the column data length, and the column data:

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, open a cursor, and assign 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;

    /* Get a number of total columns */
    EXEC SQL GET DESCRIPTOR d :d_count = COUNT;
    printf("d_count                 = %d\n", d_count);

    /* Get 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);

    /* Closing */
    EXEC SQL CLOSE cur;
    EXEC SQL COMMIT;

    EXEC SQL DEALLOCATE DESCRIPTOR d;
    EXEC SQL DISCONNECT ALL;

    return 0;
}

执行示例后,结果将如下所示:

When the example is executed, the result will look like this:

d_count                 = 1
d_returned_octet_length = 6
d_data                  = testdb

Compatibility

GET DESCRIPTOR 在 SQL 标准中指定。

GET DESCRIPTOR is specified in the SQL standard.