I have stored procedures written in CLR (C#) that are sitting in SQL Server DB. When I run those procedures I get return value as well as output. Procedure code is:
using(SqlConnection conn = new SqlConnection("context connection=true"))
{
SqlPipe pipe = SqlContext.Pipe;
conn.Open();
string table = tableName.ToString();
string columns = columnList.ToString();
string values = valueList.ToString();
string types = typeList.ToString();
cmd.Connection = conn;
cmd.CommandText = @"
INSERT INTO " + table + " (" + columns + ") VALUES(" + values + ");SELECT SCOPE_IDENTITY();";
pipe.ExecuteAndSend(cmd);
return 48;
Now when I run this code in SQL Management I get:
now in PHP app I want to access either SCOPE_IDENTITY() value sent to output or custom return value (both will work in my case).
$params = array(
array(&FW::$session->requestUniqueId, SQLSRV_PARAM_IN),
array(&$this->rootTable, SQLSRV_PARAM_IN),
array(&$columns, SQLSRV_PARAM_IN),
array(&$values, SQLSRV_PARAM_IN),
array(&$types, SQLSRV_PARAM_IN)
);
$sql="EXEC sp_data_insert @requestId=?, @tableName=?, @columnList=?, @valueList=?, @typeList=?";
$prep=sqlsrv_prepare($this->conn,$sql,$params);
if($prep!==false)
{
$res=sqlsrv_execute($prep);
if($res)
{
//HOW TO ACCESS RETURN VALUE OR OUTPUT VALUE YIELD BY SQL PROCEDURE HERE ?
}
}
Can anyone tell me how to access return value 48 returned by CLR stored procedure or/and output value (SCOPE_IDENTITY)?
I know I can add output paramter but this would be a pain, since I would have hundrets of procedures to change. I need to access these values without changing SQL CLR.
Problem is that I cannot make this work in PHP. What can be wrong? This code does execute the stored procedure (I see data in DB changed), but I cannot access return value in PHP:
$sql="DECLARE @ret int EXEC @ret=$stored_procedure; SELECT @ret";
$prep=sqlsrv_prepare($this->conn,$sql,$params);
if($prep!==false)
{
$res=sqlsrv_execute($prep);
if($res)
{
while($row=sqlsrv_fetch_array($prep))
{
print('row='.print_r($row,true));
}
}
}