我正在尝试从php执行sql server存储过程。存储过程返回输出值。我希望php中的值显示在网页中。
一切正常,除了输出值在php中返回0 ..但是行完全插入了。输出值显示为0.在我犯错误的地方生气。下面是我的PHP代码
require('db.php');
$billno = "REF0001";
$retsno = 0.0;
$sp_name = "{call testinsert( ?, ? )}";
$params = array(
array($billno, SQLSRV_PARAM_IN),
array($retsno, SQLSRV_PARAM_OUT)
);
/* Execute the query. */
$stmt3 = sqlsrv_query( $conn, $sp_name, $params);
if( $stmt3 === false )
{
echo "Error in executing statement 3.\n";
die( print_r( sqlsrv_errors(), true));
}
echo "Inserted Retuern Sno for ".$billno." is ". $retsno. ".";
我的存储过程是
create procedure testinsert(@bill_no VARCHAR(20),@RetSno INT OUTPUT)
as
begin
insert into testable values(@bill_no,GETDATE())
set @RetSno = @@IDENTITY
return
end
答案 0 :(得分:0)
也许你错过了可滚动。 https://msdn.microsoft.com/en-us/library/hh487160(v=sql.105).aspx
select m.data,
ISNULL(((sum(case when m.status = 'ok' then 1 else 0 end)
*100)/count(m.status)), 0) percentage
from myTable m
where date between '2017-01-20' and '2017-01-22'
group by m.data
答案 1 :(得分:0)
在使用echo
之前,请添加以下行以阅读下一行:
sqlsrv_fetch($stmt3);
然后,您可以使用sqlsrv_get_field()
选择响应的第一个字段,该字段应该是您的参数:
$id = sqlsrv_get_field(0);
答案 2 :(得分:0)
我遇到了同样的情况,并使用以下方法解决了这个问题:
sqlsrv_next_result($stmt);
在指令结束时。在这里,我将部分代码放在以防万一的情况下。
$totalPaginas = 0;
$result = array();
$query = "EXEC DBO.SPS_EJECUTAR_CONSULTA @ReporteID = ?, @FilasPorPagina = ?, @NroPagina = ?, @TotalPaginas = ?";
$params = array(
array(&$reporteid, SQLSRV_PARAM_IN),
array(&$filasPorPagina, SQLSRV_PARAM_IN),
array(&$nroPagina, SQLSRV_PARAM_IN),
array(&$totalPaginas, SQLSRV_PARAM_OUT)
);
$stmt = sqlsrv_prepare($this->db, $query, $params);
if( !$stmt ) {
// show errors
}
$result = sqlsrv_execute($stmt);
if( !$result ) {
// show errors
}
$data = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$data[] = $row;
}
sqlsrv_next_result($stmt);
var_dum($data);
echo('Total Paginas: '.$totalPaginas);
正式告诉你如何在这里做: https://docs.microsoft.com/en-us/sql/connect/php/how-to-retrieve-input-and-output-parameters-using-the-sqlsrv-driver