Note that there are some explanatory texts on larger screens.

plurals
  1. POPDO with MSSQL driver how to get output parameters?
    primarykey
    data
    text
    <p>When you are using <a href="http://php.net/manual/ru/book.pdo.php" rel="noreferrer"><code>PDO</code></a> with <a href="http://www.php.net/manual/ru/ref.pdo-sqlsrv.php" rel="noreferrer">MSSQL driver</a> you actually use <a href="http://www.freetds.org/" rel="noreferrer">FreeTDS</a> as low level driver. There is some different ways to execute stored procedures - <strong>language queries</strong> and <a href="http://www.freetds.org/tds.html" rel="noreferrer"><strong>RPC call</strong></a>.</p> <p>FreeTDS also supports TDS protocol version 4.2 and 7.x. The one of main difference between them is a behaviour of stored procedure call. Microsoft changed the behaviour from protocol 4.2 to 7.0 not returning output parameters from language queries. <strong>Language queries</strong> mainly send the textual query to the server wrapping into a TDS packet. </p> <h3>Example of language query with PDO (from <a href="http://www.php.net/pdo.prepared-statements" rel="noreferrer">php.net</a>)</h3> <pre><code> $stmt = $dbh-&gt;prepare("CALL sp_takes_string_returns_string(?)"); $value = 'Hello!'; $stmt-&gt;bindParam(1, $value, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000); $stmt-&gt;execute(); print "The output is $value\n"; </code></pre> <p>Actually you send something like "EXEC sp_takes....". And if you run sample above with MSSQL you would get empty output parameter in TDS 7.х and expected result with 4.2. Why we can`t use 4.2 and be happy? It has a lot of <a href="http://www.freetds.org/userguide/choosingtdsprotocol.htm" rel="noreferrer">limitations</a>:</p> <ul> <li>ASCII only, of course. </li> <li>RPC and BCP is not supported.</li> <li>varchar fields are limited to 255 characters. If your table defines longer fields, they'll be truncated. </li> <li>dynamic queries (also called <code>prepared statements</code>) are not supported.</li> </ul> <p>So, the 4.2 is not a variant. </p> <h3>Example of RPC call (from <a href="http://docs.php.net/manual/en/function.mssql-execute.php" rel="noreferrer">php.net odbtp extension</a>)</h3> <pre><code> $stmt = mssql_init('sp_takes_string_returns_string'); $value = 'Hello!'; mssql_bind($stmt, 1, $value, SQLVARCHAR, true, false, 4000); mssql_execute($stmt); print "The output is $value\n"; </code></pre> <p>Using sample above with native mssql extension in php you got right result with TDS 7.2. Actually you send binary RPC packet with that code.</p> <h2>Question</h2> <p>Is there is any way to make RPC call for stored procedure with PDO and MSSQL driver?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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