dynamic sql - prepare statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Massimiliano Campagnoli

    dynamic sql - prepare statement

    Hy,
    I am trying to understand how the prepare statement works.

    I am using Qt C++ library to access db2:

    {
    ....
    QSqlQuery cust_query;

    cust_query.prep are("SELECT CUST_NAME FROM CUSTOMERS WHERE CUST_ID=?");

    cust_query.addB indValue(1);
    cust_query.exec ();

    cust_query.addB indValue(2);
    cust_query.exec ();
    ....
    }

    db2 precompiles the select statement first, then binds the parameter
    CUST_ID to value 1 and executes the query, then binds the parameter
    CUST_ID to value 2 and again executes the query.

    So far so good.

    But what happens if prepare is executed a second time against the same
    sql statement ?
    Do I pay the cost of re-preparing the statement again or it's now
    cached into db2 and it's does not matter if prepare is executed more
    than once against the same statement ?

    Thanks for the help.
  • Mark A

    #2
    Re: dynamic sql - prepare statement

    "Massimilia no Campagnoli" <maxi@paoloasto ri.comwrote in message
    news:ef9aad37-6f53-4e17-a90b-b55150ac8b69@p3 9g2000hsb.googl egroups.com...
    Hy,
    I am trying to understand how the prepare statement works.
    >
    I am using Qt C++ library to access db2:
    >
    {
    ...
    QSqlQuery cust_query;
    >
    cust_query.prep are("SELECT CUST_NAME FROM CUSTOMERS WHERE CUST_ID=?");
    >
    cust_query.addB indValue(1);
    cust_query.exec ();
    >
    cust_query.addB indValue(2);
    cust_query.exec ();
    ...
    }
    >
    db2 precompiles the select statement first, then binds the parameter
    CUST_ID to value 1 and executes the query, then binds the parameter
    CUST_ID to value 2 and again executes the query.
    >
    So far so good.
    >
    But what happens if prepare is executed a second time against the same
    sql statement ?
    Do I pay the cost of re-preparing the statement again or it's now
    cached into db2 and it's does not matter if prepare is executed more
    than once against the same statement ?
    >
    Thanks for the help.
    There is a cost in the sense that you have to submit the prepare to DB2 and
    get a response back, but if the exact statement has been previously prepared
    and is still in package cache memory, then DB2 will used the cached package
    (if the statement text is identical).


    Comment

    Working...