如何在PHP中调用sqlsrv API中的远程函数?

That's pretty much the question. I made a stored function that returns a bigint, and I want the return value to be stored in a PHP variable. The script uses the sqlsrv API (since the mssql one doesn't work with SQL Server 2008). How do I do it? I can't find documentation anywhere and it seems like the call procedure doesn't work, neither does the exec one.

User Defined Functions are called on value request. It can be inside an SQL query, or within a SET command.

Example:

SET @myVariable = MyUserDefinedFunction(@parameter)
SELECT MyUserDefinedFunction(@parameter2),@myVariable

That's it. Now, in this example, you can access the result of the query, like in any other query.

By the way, next time you ask a question, be more precise:

  • Is it User Defined Functions, or Stored Procedures?
  • What kind of API are you using?
  • Some code maybe?
$stmt = sqlsrv_query($this->conn, $sql, $params);
if( $stmt == false)
{
      print_r( sqlsrv_errors());
}else{
     $result_set = sqlsrv_fetch_array($stmt);
     var_dump($result_set);
}

No???

I figured out the answer.

$query = "SELECT functionname(?,?,...,?) AS alias";
$bindvars = array($bindvar0,...,$bindvarn);
$resultset = sqlsrv_query($query, $connection, $bindvars);
$alias = sqlsrv_fetch_array($resultset);

And we get the result with $alias['alias'].