Updated on 2024-06-03 GMT+08:00

EXECUTE

Description

Executes a prepared statement. Because a prepared statement exists only in the lifetime of the session, the prepared statement must be created earlier in the current session by using the PREPARE statement.

Precautions

If the PREPARE statement declares some parameters when the prepared statement is created, the parameter set passed to the EXECUTE statement must be compatible. Otherwise, an error occurs.

Syntax

EXECUTE name [ ( parameter [, ...] ) ];

Parameters

  • name

    Specifies the name of the prepared statement to be executed.

  • parameter

    Specifies a parameter of the prepared statement. It must be an expression that generates a value compatible with the data type of the parameter specified when the prepared statement was created. ROWNUM cannot be used as a parameter.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
-- Create the reason table.
gaussdb=# CREATE TABLE reason ( 
    CD_DEMO_SK          int NOT NULL,
    CD_GENDER           varchar(10),
    CD_MARITAL_STATUS   varchar(10)
);

-- Create a prepared statement for an INSERT statement and execute the prepared statement.
gaussdb=# PREPARE insert_reason(int,varchar(10),varchar(10)) AS INSERT INTO reason VALUES($1,$2,$3);
gaussdb=# EXECUTE insert_reason(52, 'AAAAAAAADD', 'reason 52'); 

-- Query data.
gaussdb=# SELECT * FROM reason;
 cd_demo_sk | cd_gender  | cd_marital_status 
------------+------------+-------------------
         52 | AAAAAAAADD | reason 52
(1 row)

-- Delete the reason table.
gaussdb=# DROP TABLE reason;

Helpful Links

7.13.16.2-PREPARE and DEALLOCATE