SQL Execute

     
    EXECUTE <identifier> "(" <parameter-value> { , ... <parameter-value> } ")"
     

The execute statement executes an SQL query previously defined and pre-compiled by the prepare statement. This saves processor time by eliminating the statement compilation step each time the statement is executed.

The <identifier> specifies the name of the prepared statement. Parameters in the query are substituted where "?" placeholders are present in the prepared query statement. All parameters must be of type string unless explicitly type cast in the prepare statement. For example, the following prepare statement requires that an integer value be supplied for the parameter x:

 
    prepare lookup as select * from t where x=?::integer;
    execute lookup(1);
            
     

The following example statements demonstrate defining and executing prepared statements:

 
    create table t(x integer primary key, y integer);
    prepare lookup as select * from t where x=?::integer;
    prepare lookup2 as select * from t where x=?::integer and y=?::integer;
    insert into t values (1,10);
    execute lookup(1);
            
    explain execute lookup(1);
    execute lookup2(1,10);
            
    explain execute lookup2(1,10);
    prepare selfjoin as select * from t as t1 left join t as t2 on t1.y=t2.y;
    execute selfjoin();
            
    explain execute selfjoin();
    execute selfjoin();