Initializes a query written in SQL language and declares this query to the database server in order to optimize the next executions of this query. This query is not run. Then, the query can be run with HExecuteSQLQuery
. This function is available for queries with or without bind.
We recommend that you use this function when the same query is run successively and when only some of the query parameters are modified at each execution.
This function is optional and it can only be used on the Client/Server databases (available for Oracle, Oracle Lite, Sybase and SQL Server). In SQL Server, Native Connector via OLE DB or ODBC may be required.
For the other databases accessed by a Native Connector, OLEDB or ODBC, HPrepareSQLQuery runs the query.
This function cannot be used on the HFSQL databases (HFSQL Classic or Client/Server) and on the xBase databases.
// Run the same query in Oracle
// Declare a data source
// This data source corresponds to the query
Insert1 is Data Source
i is int
// Declare one of the query parameters
// This parameter is an integer
Insert1.age = 0
// Prepare the query for multiple executions
HPrepareSQLQuery(Insert1, DatabaseConnection, ...
hQueryWithoutCorrection, "INSERT INTO PERSON VALUES (:lastname,:firstname,:age)")
// Loop for running the query
// Only some parameters are modified
FOR i = 1 TO 10
Insert1.lastname = "LastName" + i
Insert1.firstname = "FirstName" + i
Insert1.age = i
<Result> = HPrepareSQLQuery(<Data source> , <Connection> , <Mode> , <SQL query text>)
- True if no problem occurred,
- False otherwise. HErrorInfo returns more details about the problem.
<Data source>: Data source
Name of the Data Source variable that corresponds to the request to initialize.
<Connection>: Character string or Connection variable
Name of connection used to run the query. This connection corresponds to: <Result> is set to False if this parameter does not correspond to an existing connection.
<Mode>: Integer constant
|hQueryWithoutCorrection||Native Connector only: No check is performed by the HFSQL engine on the query text.|
<SQL query text>: Character string
Text of the SQL query to execute.
When to use HPrepareSQLQuery?
In some cases, it may be interesting to run the same query several times while modifying one or more variables. For example, you may want to run an Insert query several times to add several records into a file.
Several solutions can be implemented:
- Execute the query directly (with HExecuteSQLQuery) as many times as necessary, and modify the desired variable(s) each time.
- Prepare the query to be executed (HPrepareSQLQuery) as well as the different variables to be modified. Then, execute the query as many times as necessary with HExecuteSQLQuery. This solution is much faster and optimizes the time it takes to loop through the result of a query (Select query).
SQL Server: Limit
cannot be used to prepare a stored procedure that returns records. You must use HExecuteSQLQuery
How to use HPrepareSQLQuery?
To prepare and execute a query multiple times:
- Declare a data source. This data source will contain the result of the SQL query.
- Declare the different variables of the query.
The variables are string variables by default. You can specify their type by using the Type property with the variable.
- Prepare the query with HPrepareSQLQuery.
- Specify the value of the different parameters to take into account and run the query with HExecuteSQLQuery. Only the name of the data source that corresponds to the query must be specified.
This last step must be repeated as many times as necessary.
- HPrepareSQLQuery must be used with:
- the connection name,
- the hQueryWithoutCorrection constant.
- The declared variables must be identical to the ones used. Otherwise, a WLanguage error occurs.
- In the call to the stored procedure, you must use the syntax specific to the database used, including for the syntax of parameters.
Therefore, for Oracle, the parameters are specified with the :ParamName syntax. Caution: the ":" character must be followed by at least one letter (the syntax:1 is not allowed).
In SQL Server, the parameters are specified via the following notation: @ParamName.
The same parameter can be used several times. In this case, the corresponding variable will be reused.
Condition on a composite key in an SQL query
To define a condition on a composite key in an SQL query, the conditions must be specified for each component of the key.
Do not attempt to directly assign a value to the composite key (indeed, the composite keys are stored as binary values).
Example: The composite key is made of LASTNAME and FIRSTNAME items (LASTNAMEFIRSTNAME item):
SELECT AFile.AnItem, AFile.AnItem1
WHERE AFile.LastName = "Clark" AND AFile.FirstName = "Vince"
This function is not available when using a database via Remote Access