Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3>A Minor Diversion</h3> <p>You have some syntactic problems in what you show us:</p> <pre><code>sprintf(sqlQuery, "select distinct e.emp_id, e.join_date \ from employees e where e.emp_id in (?) and e.dob &lt;= '%s')"); </code></pre> <p>That won't compile; you'd need a second backslash after <code>employees e</code>. I strong recommend avoiding backslash-newline in strings; use string concatenation instead.</p> <pre><code>sprintf(sqlQuery, "select distinct e.emp_id, e.join_date from employees e " "where e.emp_id in (?) and e.dob &lt;= '%s')"); </code></pre> <p>Note that there is just white space (comments would count as white space too) between the two parts of the string; the C compiler concatenates such strings into a single string.</p> <p>Now the <code>sprintf()</code> call is syntactically correct at the C source level; it is still semantically incorrect because it contains <code>%s</code> and you've not provided a string for it to copy. You should presumably be using a placeholder <code>?</code> for the date since you pass it to the <code>$open</code> statement as a second parameter (but there isn't actually a placeholder for it).</p> <p>You would then be able to avoid an explicit prepare operation by writing:</p> <pre><code>$ DECLARE empDataCursor FOR SELECT DISTINCT e.emp_id, e.join_date FROM employees e WHERE e.emp_id IN ($empIds_) AND e.dob &lt;= $dob_; </code></pre> <hr> <h3>The Crux</h3> <p>However, this isn't going to work for you, unfortunately. The crux of your problem is you are trying to pass a string as a list of values for the IN clause. It simply doesn't work like that. If you have one value, you need one placeholder (<code>?</code>); if you have two values, you need two placeholders, etc.</p> <p>So, we end up going back to a full prepared statement and substitute the empIds into the string:</p> <pre><code> int reqlen; reqlen = snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DISTINCT e.emp_id, e.join_date" " FROM employees e WHERE e.emp_id IN (%s) AND e.dob &lt;= '%s'", empIds_, dob_); if (reqlen &gt;= sizeof(sqlQuery)) ...truncated SQL...larger sqlQuery needed... $ PREPARE empDataStmt FROM $sqlQuery; ...SQL error check... $ DECLARE empDataCursor FOR empDataStmt; ...SQL error check... $ OPEN empDataCursor; /* No USING clause! */ ...SQL error check... ...code as before... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload