Connecting Tech Pros Worldwide Help | Site Map

dynamic sql - prepare statement

  #1  
Old October 27th, 2008, 08:35 AM
Massimiliano Campagnoli
Guest
 
Posts: n/a
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.prepare("SELECT CUST_NAME FROM CUSTOMERS WHERE CUST_ID=?");

cust_query.addBindValue(1);
cust_query.exec();

cust_query.addBindValue(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.
  #2  
Old October 27th, 2008, 08:45 AM
Mark A
Guest
 
Posts: n/a

re: dynamic sql - prepare statement


"Massimiliano Campagnoli" <maxi@paoloastori.comwrote in message
news:ef9aad37-6f53-4e17-a90b-b55150ac8b69@p39g2000hsb.googlegroups.com...
Quote:
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.prepare("SELECT CUST_NAME FROM CUSTOMERS WHERE CUST_ID=?");
>
cust_query.addBindValue(1);
cust_query.exec();
>
cust_query.addBindValue(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).


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# APP Generate Dynamic SQL Statement from ListBox sql2020 answers 3 December 5th, 2008 02:53 PM
How do I set combobox data source to a dynamic SQL statement? Newbie answers 2 October 11th, 2006 07:05 PM
VB.NET: ComboBox in dynamic SQL Statement Question italian464 answers 1 August 13th, 2006 08:23 PM
Help with dynamic SQL Stored Procedure Caro answers 2 July 23rd, 2005 09:41 AM
Error in dynamic sql: Data type mismatch in criteria Jack answers 7 July 22nd, 2005 01:54 AM