read or readAll Error database

I am working on Mssql server and get this error


<p>SQLSTATE[IMSSP]: The active result for the query contains no fields.</p>

, when I use read() or readAll().Here is my code

            $sql = "        DECLARE	@StartDate DATETIME

                            DECLARE	@EndDate DATETIME

                            DECLARE @dateDiff int

                            SELECT @StartDate = '$start_day', @EndDate = '$end_day'

                            SELECT @dateDiff = DATEDIFF(DAY, @StartDate, @EndDate)

                            DECLARE	@Number TABLE


                                            Number INT IDENTITY(1,1) PRIMARY KEY CLUSTERED 		


                            WHILE COALESCE(SCOPE_IDENTITY(), 0) <= @dateDiff 


                                INSERT @Number DEFAULT VALUES 


                            DECLARE @MESSAGES TABLE


                                    day DATETIME	



                            INSERT @MESSAGES 

                                SELECT DATEADD(DAY, N.Number, @StartDate)

                                FROM @Number as N    

                                WHERE N.Number <= @dateDiff

                                    ORDER BY N.Number





                            cast( DATEPART(dayofyear,( as int)  as DayOfYear,

                            cast( DATENAME ( month , ( ) as nchar) as thisMonth,

                            cast ( DATENAME ( day , ( ) as int) as thisDay,

                            cast ( COALESCE(sum(DATEDIFF(minute, d.login_time, isnull(d.logout_time, d.last_keypress) ) ),0) as int) as TotalMinutes

                            FROM  @MESSAGES t 

                            LEFT OUTER JOIN echo_logins d on DATEPART(dayofyear,(d.Login_Time)) = DATEPART(dayofyear,(

                            GROUP BY DATEPART(dayofyear,(, DATENAME( month , ( ), DATENAME ( day , ( )




$reader = Yii::app()->db->createCommand($sql)->query();



             if ( $reader->rowCount > 1 ){

                  $test = $reader->readAll();


             }while ( $reader->nextResult() ) ;

i think you 'd better to create a stored procedure (function ) on server side ,then call if from php .

because the yii use pdo underline so please refer to how pdo to access server end procedure:

prepare stored procedure and access it

and discuss for accessing stored procedure from pdo

I will be creating stored procedure on the database side, but I just wanted to give someone a hint as to what I am doing. Now, I can call to this stored procedure wihthout any problem using custom code.

conn = sqlsrv_connect("localhost\sqlexpress", $connectionInfo);        


        if ($conn){

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

            $count = 0;


                if ( sqlsrv_fetch($stmt) ){

                    while ( $array = sqlsrv_fetch_array($stmt) ){


                        $result[$count]['DayOfYear'] = $array['DayOfYear'];

                        $result[$count]['TimePeriod'] = $array['thisMonth']." - ".$array['thisDay'] ;                        

                        $result[$count]['TotalMinutes'] = $array['TotalMinutes'];





              }while ( sqlsrv_next_result($stmt) ) ;    


        else {

            echo "couldn't set connection";


But I should be able to do the same, using Yii. But it gives me an error while doing it. I want to make sure I am using the right method for this…