0

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?

3
  • Since you are using sql 2012 have you looked at using the built in sequence? It is far less prone to error than this type of roll your own version. msdn.microsoft.com/en-us/library/ff878091.aspx
    – Sean Lange
    Aug 20, 2014 at 19:24
  • Hi Sean, thanks for the reply. I haven't looked into the built in sequence - but I will. Although, if it's returning a val from a proc then it still won't work with my code (I am guessing :-))
    – nashleigh
    Aug 20, 2014 at 19:29
  • Yeah I can help with the sql part but I can barely spell php let alone write a single line of code in it. :D
    – Sean Lange
    Aug 20, 2014 at 19:55

1 Answer 1

1

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

3
  • $outSeq is set to 0 - int, the output in the article you refer to is money. My output type on the SP is set to INT (spGetNextSeqID @ID AS INT OUTPUT). I referenced the article and I'd already gone through it before posting here. Nothing helped. I am still getting nothing back.
    – nashleigh
    Aug 20, 2014 at 19:48
  • I didn't need to change array($outSeq, SQLSRV_PARAM_INOUT). I left it as array($outSeq, SQLSRV_PARAM_OUT). The problem as you pointed out was that I wasn't moving through the results that came back - I assumed (quite amateurishly) that it would simply pick up the returned value - but thinking about it now, it makes perfect sense as to why it wasn't working.
    – nashleigh
    Aug 20, 2014 at 20:38
  • Sounds good.. I'm glad you can now move forward with your project.
    – Ahz
    Aug 20, 2014 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.