Tuesday, February 8, 2011

Can PHP's SQL Server driver return SQL return codes?

Stored procs in SQL Server sometimes finish with a return code, as opposed to a recordset of data. I've seen ASP code that's able to get this return code, but I can't figure out how to get this code with PHP's mssql driver.

mssql_get_last_message() always returns nothing, and I'm thinking it's because it only returns the very last line that came from the server. When we run the proc in another application (outside PHP), there is blank line following the return code.

Has anyone figured out how to get return codes from SQL stored procs using PHP's mssql driver?

  • Are you talking about SQL Server error codes, e.g. RAISERRROR or other failures? If so, last time I checked in PHP you need to ask for @@ERROR (e.g. select @@error) instead.

    If it is a return code, you must explicitly catch it, e.g.

    DECLARE @return_code INT
    EXEC @return_code = your_stored_procedure 1123
    SELECT @return_code
    
    Aston : I think this works, thanks!
  • To get a numeric error code from mssql you can do a select that looks something like

    SELECT @@ERROR AS ErrorCode
    

    Which SHOULD return the correct error code.

    From Mez
  • I tried using @@ERROR and I keep getting 0 as the result, but when I run the exact same proc in Query Analyzer with the exact same parameters, I get return code = 1.

    From Aston

0 comments:

Post a Comment