Book Image

PostgreSQL Server Programming - Second Edition

Book Image

PostgreSQL Server Programming - Second Edition

Overview of this book

Table of Contents (21 chapters)
PostgreSQL Server Programming Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Accessing databases


PL/Tcl functions provide you with SPI functions to access the database and run DML/DDL statements.

The functions are the following:

  • spi_exec: This executes a SQL statement

  • spi_prepare: This prepares a SQL statement

  • spi_execp: This executes a prepared statement

The spi_exec function has the following syntax:

spi_exec ?-count n? ?-array name? command ?loop-body?

The spi_exec function runs a SQL statement, and it takes some optional parameters, as follows:

  • -count: This parameter allows you to specify the maximum number of rows processed by the command. If you provide the value 3, only 3 rows will be processed. This is similar to specifying FETCH [n] in a cursor.

  • -array: If this parameter is specified, the column values are stored into a named associative array and the column names are used as array indexes. If this parameter is not specified, the result values are stored in the Tcl variables of the same name.

If there is a loop body specified, then it is treated as a script that is...