Note that there are some explanatory texts on larger screens.

plurals
  1. POMS-SQL Server search table using assembled criteria
    text
    copied!<p>I have a search page that returns results according to the criteria nominated and this works ok when each criteria is OR but when I use AND it returns bad or no results. For example the search criteria might be...</p> <pre><code>A. Author = "" B. Subject = "" C. Keyword = "" D. Dated = "" </code></pre> <p>Code:</p> <pre><code>SELECT * FROM Table WHERE Author = '" &amp; strAuthor &amp; "' AND Subject = '" &amp; strSubject &amp; "' AND Keyword = '" &amp; strKeyword &amp; "' AND Dated = '" &amp; strDated &amp; "' " </code></pre> <p>Here I have used only 4 parameters whereas in fact there are quite a few more. But the example should explain the problem... to make this work I would need to be more specific such as "if A and B" or "B, C and D" but using any parameters that are blank or NULL will not work.</p> <p>Now I could write in the criteria using a conditional statement like...</p> <pre><code>SELECT * FROM Table WHERE if strAuthor &lt;&gt; "" then Author = '" &amp; strAuthor &amp; "' end if if strSubject &lt;&gt; "" then AND Subject = '" &amp; strSubject &amp; "' end if </code></pre> <p>and so on, except that writing <code>SELECT</code> strings like this does not work, just produces errors because the select string cannot contain additional code (that has been my finding).</p> <p>If the options were only a few I could write separate select strings for each combination, but there are more than 10 different criteria which entail more than 3,628,800 combinations!</p> <p>Is there a solution for this?</p>
 

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