doupao1978 2014-08-20 19:12
浏览 47
已采纳

PHP从MSSQL存储过程获取返回值

I can't get the OUTPUT parameter from my SQL Server (MSSQL 2012) SP to return to PHP. My Stored procedure is:

CREATE PROCEDURE spGetNextSeqID @ID AS INT OUTPUT
AS
BEGIN
    BEGIN TRANSACTION
    SELECT @ID = SEQUENCE_NO + 1 FROM tblCSRSequence WITH (TABLOCKX)
    UPDATE tblCSRSequence SET SEQUENCE_NO=@ID
    COMMIT TRANSACTION
END

And my PHP code is:-

<?php
include "DBConnect.php";
$conn = sqlsrv_connect( $serverName, $connection);
if( !$conn )
{
     echo "Connection could not be established to ".$serverName;
     die( print_r( sqlsrv_errors(), true));
}
$sql="{call dbo.spGetNextSeqID( ? )}";
$outSeq=0;
$params = array
    (
        array($outSeq, SQLSRV_PARAM_OUT)
    );

$stmt = sqlsrv_query( $conn, $sql, $params );

if( $stmt == false)
    die( print_r( sqlsrv_errors(), true) );

sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn );
echo $outseq;
?>

I know the SP is getting called and working - I checked it with a trace and can see that it's generating the following:-

declare @p1 varchar(max)
set @p1='154'
exec dbo.spGetNextSeqID @p1 output
select @p1

Each time I refresh my browser page it calls the SP and increments the counter by 1 but never returns the value to the calling PHP function. I've been fiddling with this for about 2 days now - I've scoured the similar posts but none of the suggested fixes (like SET NOCOUNT ON etc) work.

Anyone got any ideas?

  • 写回答

1条回答 默认 最新

  • drxnfdx798517235 2014-08-20 19:38
    关注

    New:

    I missed that you are using a single parameter as both input and output. Please try the following.

    array($outSeq, SQLSRV_PARAM_INOUT)
    

    Then using

    sqlsrv_next_result($stmt);
    echo $outSeq;
    

    Reference: http://technet.microsoft.com/en-us/library/cc644932(v=sql.105).aspx

    Old:

    You must set up $outSeq with the appropriate data type. Try initialize the value to $outSeq = 0.00, since your output type is MONEY.

    Please reference the following article:

    http://technet.microsoft.com/en-us/library/cc626303(v=sql.105).aspx

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看