PHP API no result from MSSQL server stored procedure when using temp table and its work when remove that TempTable

114 Views Asked by At

I have a stored proc in mssql. The SP works fine while executed directly. When I call it in PHP (PHP Version 8.1.2) I get no result.

The error in browser is:

Array ( [0] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 16954 [code] => 16954 [2] => [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Executing SQL directly; no cursor. [message] => [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Executing SQL directly; no cursor. )

)

PHP code and I use user sa with password.

<?php  

include "SQLconnectTposApp.php";
header("Content-type:application/json;charset=utf-8"); 
 
if( $conn ) {
    
     $sql =   " EXECUTE SpActionCasa  @date1=? , @date2=? , @userNo=? , @Bno =?" ; 
     $params  = array("01/01/2015", "07/07/2023", 0 ,0 );
     $options = array("Scrollable" => SQLSRV_CURSOR_KEYSET  );
     $stmt = sqlsrv_query( $conn, $sql , $params, $options );
     if( $stmt === false ) {
        die( print_r( sqlsrv_errors(), true));
     }
     $row_count = sqlsrv_num_rows( $stmt );
     $err =  array(
                'Erad'=> '0.0' ,
                'Masrof'=> '0.0' , 
                'UserDiscrption'=> ' لايوجد بيانات ' , 
                'gdate'=> array('date'=>'2022-01-01 00:00:00.000000', 'timezone_type' => 3,'timezone' => 'UTC') , 
                'doctype'=> 'لايوجد بيانات'  
     );
     if ($row_count === false)
     { 
        echo " row_count === false ";
        $json = array_fill(0, 1, $err); 
     }
     else
     {
        if ($row_count === 0) 
        {
           echo "row_count 0";
           $json =  array_fill(0, 1, $err); 
        }
        else
        {
           $json = array();
            while( $row = sqlsrv_fetch_array( $stmt , SQLSRV_FETCH_ASSOC) ) 
            {
               array_push($json ,$row);        
            } 
        }    
        echo json_encode($json); 
     } 
} else {
     echo " Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}  
    
sqlsrv_free_stmt($stmt); 
sqlsrv_close($conn); //Close the connnection first

?> 

This is the stored procedure. The problem is in #temp Table it work in php api when i remove the temp table

ALTER PROCEDURE [dbo].[SpActionCasa] 
@date1 datetime,@date2 datetime , @userNo int, @Bno int
 
AS
BEGIN

create table #TempActCasa(
   gdate datetime NULL ,
   daen numeric(18,3) NULL,
   maden numeric(18,3) NULL,
   rased numeric(18,3) NULL,
   vWhy nvarchar(170) NULL,
   idnum int NULL,
   flg smallint NULL,
   vtype nvarchar(25) NULL,
   userNo int NULL,
   optime Datetime NULL 
)
   
    
insert into #TempActCasa (gdate, daen, maden,rased,vWhy,idnum,vtype, userNo , optime)
SELECT wasDate, WasValue, 0, 0, CostName+' '+wassabb , WasNo , 'ايصال دفع'   , userNo , operation_time 
FROM TWAS 
left join TCostumer on (TWAS.WasCostNo = TCostumer.CostNo)
WHERE 
   (FType = 0) and
   (BankNo = @Bno) and
   ((wasDate >= @date1) and (wasDate <= @date2) and (userNo = ISNULL(@userNO,userno)))

insert into #TempActCasa(gdate,daen,maden,rased,vWhy , idnum, vtype, userNo , optime)
SELECT GDATE, GValue, 0, 0, sarftypeName + '  ' + sarfto, AutoNo , 'مصروفات' , userNo , operation_time 
FRom TMasrof 
left join dbo.Lsarftype on (TMasrof.sarftypeNo = Lsarftype.sarftypeNo )
   
Select * 
From #TempActCasa 
 
END

Thank you for help me!

0

There are 0 best solutions below