Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO with MSSQL driver how to get output parameters?

Tags:

When you are using PDO with MSSQL driver you actually use FreeTDS as low level driver. There is some different ways to execute stored procedures - language queries and RPC call.

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. Language queries mainly send the textual query to the server wrapping into a TDS packet.

Example of language query with PDO (from php.net)

    $stmt = $dbh->prepare("CALL sp_takes_string_returns_string(?)");     $value = 'Hello!';     $stmt->bindParam(1, $value, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000);      $stmt->execute();     print "The output is $value\n"; 

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 limitations:

  • ASCII only, of course.
  • RPC and BCP is not supported.
  • varchar fields are limited to 255 characters. If your table defines longer fields, they'll be truncated.
  • dynamic queries (also called prepared statements) are not supported.

So, the 4.2 is not a variant.

Example of RPC call (from php.net odbtp extension)

    $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"; 

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.

Question

Is there is any way to make RPC call for stored procedure with PDO and MSSQL driver?

like image 700
Nick Bondarenko Avatar asked Dec 10 '13 09:12

Nick Bondarenko


People also ask

How do you get the output of a stored procedure in a variable in SQL Server?

You can use the return statement inside a stored procedure to return an integer status code (and only of integer type). By convention a return value of zero is used for success. If no return is explicitly set, then the stored procedure returns zero. You should use the return value for status codes only.

How do I pass an output parameter to a SQL stored procedure?

The Output Parameters in Stored Procedures are used to return some value or values. A Stored Procedure can have any number of output parameters. The simple logic is this — If you want to return 1 value then use 1 output parameter, for returning 5 values use 5 output parameters, for 10 use 10, and so on.

Can function have output parameters in SQL?

You can't use OUTPUT parameters with a user defined function (UDF). By definition a scalar function just returns one scalar value. You have two options: 1 - Make this a stored procedure using OUTPUT parameters.


1 Answers

Either I missed FreeTDS when I first answered, or this question was updated. Either way...

FreeTDS Does not support output parameters in MSSQL Server 7 SP3 or greater. This is due to changes made in SQL Server.

---Old Response Below---

I know it's not exactly what you're looking for.
I ran through some old files to the only time I'd linked to MSSQL and pulled this.

$con = mssql_connect($db['host'], $db['user'], $db['pass']); mssql_select_db($db['database'], $con);//Select DB $result = mssql_query("my_Procedure_Name ".$propertyOne.", ".$propertyTwo.""); 

I hope this helps

like image 119
r007kit Avatar answered Sep 21 '22 09:09

r007kit