
session_sql_prepare_query(++Session, +ParamTemplate, +ResultTemplate, ++SQLQuery, -Cursor)

   Prepares a SQL query for execution by the DBMS.

Arguments
   Session             A session handle
   ParamTemplate       Template defining the types of the parameters (structure or [])
   ResultTemplate      Template defining the types of results tuple (structure)
   SQLQuery            A SQL query in prepared syntax (string)
   Cursor              Returned cursor handle

Type
   library(dbi)

Description

 Prepares a SQL query for execution. The query is not actually
 executed, and a cursor_next_execute/2 needs to be called to execute the 
 SQL query, giving values to any parameters. Then the cursor_*_tuple family
 of predicates can be used to obtain the results. This facility is only
 available if the DBMS supports prepared statements, and the SQL query has
 to be written in the prepared statement syntax of the DBMS. The predicate
 returns the cursor handle representing this prepared query in Cursor,
 which can then be used in subsequent library predicates.

 The SQL statement must be a query, i.e. it must return results. If
 the SQL statement is not a query, an error will be raised.

 A prepared SQL statement is parsed by the DBMS, so that it could be
 executed more efficiently. It can also be parameterised, where the 
 parameters represent values to be filled in when the statement is
 executed. The statement can be executed multiple times with different
 parameters. The types of the parameters is specified by ParamTemplate,
 which is a Prolog structure of arity M (where M is the number of
 parameters for the statement), or the nil atom [] if there are no parameters.
 See the general description of this library or the manual for a
 description of the template specification.

 The SQL query returns result in tuples of N elements each. Each tuple is
 mapped to a Prolog structure of arity N. ResultTemplate is a structure of
 arity N specifying the types of the return data for ECLiPSe. If N
 does not match the number of elements expected for the query, an
 error will be raised. See the general description of this library or
 the manual for a description of the template specification.

 The SQL query must be valid for the DBMS to execute. It can contain
 NULL characters, i.e. it can contain binary data.

 Note that some DBMS restricts which SQL statements can be prepared. If an
 SQL statement cannot be prepared, it can still be executed using
 session_sql/3.

 MySQL specific note: not all SQL statements can be prepared by MySQL.
 Refer to the MySQL manual for details.


Exceptions
     5 --- Session is not a valid session handle, or SQLQuery not a string, or ResultTemplate or ParamTemplate not a structure
   dbi_error --- Error from DBMS while preparing SQLQuery.
   dbi_bad_template --- ResultTemplate or ParamTemplate has the wrong arity

Examples
   
 make_check_overdraft_limit(Session, Cursor) :-
      % note '?' in SQL in the syntax MySQL uses for placeholders. This may be
      % different in other DBMS
      SQL = "select count(id) from accounts where ID = ? \
                 and balance < overdraft",
      session_sql_prepare_query(Session,a(0),c(0),SQL,Cursor).

See Also
   cursor_next_execute / 2, cursor_next_tuple / 2, cursor_all_tuples / 2, cursor_N_tuples / 4, session_sql / 3, session_sql_query / 4
