title: MSSQL parameterized query causes "Incorrect syntax near the keyword 'from'"
body: <p>I have a parameterized query that looks like this:</p>
  
  <pre><code>SELECT title, html, shortname FROM dbo.Videos
  WHERE Topic_ID = ? AND dbo.gradeLevelCheck(?, Grade_Lower, Grade_Upper) = 1
  ORDER BY shortname ASC
  </code></pre>
  
  <p>When I run it from ASP, I get an error that reads:</p>
  
  <pre><code>Incorrect syntax near the keyword 'from'
  </code></pre>
  
  <p>The parameters are <code>56</code> and <code>1</code> (so no nulls). The stored function <code>dbo.gradeLevelCheck</code> looks like this:</p>
  
  <pre><code>ALTER FUNCTION [dbo].[gradeLevelCheck]
  (
      @value int,
      @lower int,
      @upper int
  )
  RETURNS int
  AS
  BEGIN
      DECLARE @result int;
      IF (@lower IS NULL OR @lower &lt;= @value) AND (@upper IS NULL OR @upper &gt;= @value)
          SET @result = 1;
      ELSE
          SET @result = 0;
      RETURN @result;
  END
  </code></pre>
  
  <p>It works fine if I submit it with the parameters hardcoded into the query. It also works fine if I remove the ORDER BY clause. But I can't get it to work in this form. Can anyone help me figure out why?</p>
  
  <p>EDIT: Here are some additional details. The application is written in Classic ASP with JScript. The connection is established thus:</p>
  
  <pre><code>        var conn = Server.CreateObject('ADODB.Connection');
          var connectionString =
              'Provider=SQLOLEDB;' +
              'Data Source=' + Settings.dbServer + ';' +
              'Initial Catalog=' + Settings.dbCatalog + ';' +
              'User Id=' + Settings.dbUser + ';' +
              'Password=' + Settings.dbPass + ';' +
              'Connect Timeout=15;';
          conn.Open(connectionString, conn);
  </code></pre>
  
  <p>The code used to submit the query looks like this:</p>
  
  <pre><code>    DB.query = function(query) {
      ...
              var cmd = Server.CreateObject("ADODB.Command");
              cmd.activeConnection = conn;
              cmd.commandText = query;
              for (var i = 1; i &lt; arguments.length; i++) {
                  cmd.Parameters(i-1).value = arguments[i];
              }
              return cmd.Execute();
      ...
      }
  </code></pre>
  
  <p>(Here, the parameters <code>1</code> and <code>56</code> are passed as additional arguments to <code>DB.query</code>.)</p>