简体   繁体   English

在使用sqlsrv(PHP)的SP中遇到输出参数问题

[英]Having trouble with OUTPUT paramaters in SP with sqlsrv (PHP)

Sorry for my poor english. 对不起,我英语不好。 Here's the problem: i'm trying to reciebe an ID in PHP from a table in sql server express 2014 via sqlsrv driver, an SP and a output parameter. 这是问题所在:我正在尝试通过sqlsrv驱动程序,SP和输出参数从sql server express 2014中的表中获取PHP中的ID。

When i execute de SP and print the output parameter via SSMS everything works just fine. 当我执行de SP并通过SSMS打印输出参数时,一切正常。 However, when i call the SP from PHP the SP is executed but the output parameter doesn't change. 但是,当我从PHP调用SP时,将执行SP,但输出参数不会更改。

This is the SP: 这是SP:

ALTER PROCEDURE [dbo].[SiguientePaciente](
    @IdExamen int OUTPUT
) AS
BEGIN
    SET NOCOUNT ON

    SET @IdExamen = (SELECT TOP 1 T.IdExamen FROM TURNOS T WHERE T.Estado = 0 ORDER BY T.FechaHora ASC)

    UPDATE TURNOS SET Estado = 1 WHERE IdExamen = @IdExamen

    RETURN
END

This is the PHP, assume that $conexion works: 这是PHP,假设$conexion有效:

$idExamen = 0;
$sql = "{CALL SiguientePaciente (?)}";
$params = [&$idExamen, SQLSRV_PARAM_OUT, SQLSRV_PHPTYPE_INT, SQLSRV_SQLTYPE_INT];
$stmt = sqlsrv_query($conexion, $sql, $params);
if($stmt !== false){
    //sqlsrv_next_result($stmt);
    echo $idExamen;
}
else
    echo print_r(sqlsrv_errors(), true);

In the end, echo $idExamen prints 0 最后, echo $idExamen打印0


EDIT: found an alternative way to do what i want, with a SELECT inside the SP. 编辑:找到一种替代方法来做我想做的事,在SP内部使用SELECT。 Then i just fetch de result in PHP 然后我只是在PHP中获取结果

ALTER PROCEDURE [dbo].[SiguientePaciente](
    @IdExamen int OUTPUT
) AS
BEGIN
    SET NOCOUNT ON

    SET @IdExamen = (SELECT TOP 1 T.IdExamen FROM TURNOS T WHERE T.Estado = 0 ORDER BY T.FechaHora ASC)

    UPDATE TURNOS SET Estado = 1 WHERE IdExamen = @IdExamen

    SELECT @IdExamen AS IdExamen
    RETURN;
END

At this msdn link there is another way to assign your php $params array. 在此msdn链接上,还有另一种分配您的php $params数组的方法。 I try to adapt that form to your case: 我会尝试根据您的情况调整表格:

$params = array(   
                 array($idExamen, SQLSRV_PARAM_OUT)  
               );   

It seems that doesn't need to apply referencing operator (I think) to the output variable when passing it, and other php constants related to the parameter.. 似乎在传递输出变量以及与该参数相关的其他php常量时,不需要将引用运算符(我认为)应用于输出变量。

update after comment 评论后更新

try also 也尝试

SELECT TOP 1 @IdExamen = T.IdExamen FROM TURNOS T WHERE T.Estado = 0 ORDER BY T.FechaHora ASC

it is a bit more compact.. 它更紧凑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM