PDO返回的结果集不同于MS-SQL

时间:2017-12-27 16:32:37

标签: php sql sql-server pdo

我有一个存储过程可以返回工资单和每天的总工时。当我在MSSQL中调用存储过程时,我得到以下结果。

SANTANGELO, THOMAS  Tuesday 09:01   NULL    NULL
SANTANGELO, THOMAS  Tuesday 12:00   02.58   NULL
SANTANGELO, THOMAS  Tuesday 12:15   NULL    NULL
SANTANGELO, THOMAS  Tuesday 19:25   07.10   NULL

但是当我从PHP PDO Connection调用它时,它只会带来两个结果:

stdClass Object
(
    [name] => SANTANGELO, THOMAS
    [NM] => Tuesday
    [PunchTime] => 09:01
    [HrsAndMins] => 
    [Notes] => 
)
stdClass Object
(
    [name] => SANTANGELO, THOMAS
    [NM] => Tuesday
    [PunchTime] => 12:00
    [HrsAndMins] => 02.58
    [Notes] => 
)

以下是用于获取数据的PHP函数:

<?php
public function get_details_f($rn, $peopleId, $week, $day){

    $sql = "Exec CrossTabDetail_v3 @rn = $rn, @peopleId = $peopleId, @wk = $week, @Day = '$day'";
    echo  $sql;
    $query = $this->web_pulling_down_to_read->prepare($sql);    
    $query->execute();
    $data = $query->fetchAll(PDO::FETCH_OBJ);

    foreach($data as $array){
       echo "<pre>";
       print_r($array);
       echo "</pre>";

    } // Foreach bracket

}

编辑:这是我正在使用的SQL存储过程 -

USE [OSIstaging]
GO
/****** Object:  StoredProcedure [dbo].[CrossTabDetail_v3]    Script Date: 12/27/2017 12:13:27 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--      Exec CrossTabDetail_Dev @rn = 9, @peopleId = 11, @wk = 1, @Day = 'Monday' 
--      Exec CrossTabDetail @rn = 9, @peopleId = 11, @wk = 1, @Day = 'Monday' 


ALTER   --12.21.17
Proc    [dbo].[CrossTabDetail_v3]       --  Exec CrossTabDetail @rn = 9, @peopleId = 17, @wk = 1, @Day = 'Monday'   -- GRANT EXEC ON CrossTabDetail  TO WWW_DATA 

--Declare   
        @rn int, @PeopleId int, @wk int, @Day nvarchar(15)
as
--select    @rn = 9,
--      @peopleId = 11,
--      @wk = 1,
--      @Day = 'Monday'

Declare @prID int, @prStart date, @prEnd date,
        @wk1End date, @wk2Start date

;With prPeriod
as (
select *
from   [PayRoll].[dbo].[PayRollWeeks]
--where  rn = (Select OSIstaging.dbo.GetPayRollPeriodID(getDate()))
where  rn = @rn -- (Select OSIstaging.dbo.GetPayRollPeriodID(@CurDate))     -- grant exec on GetPayRollPeriodID to www_data
)
select  @prID = rn,
        @prStart =  prStartDate,
        @wk1End = dateadd(d,6,prStartDate),
        @wk2Start = dateadd(d,1,@wk1End),
        @prEnd = prEndDate

from    prPeriod


;With TD  -- TargetDates
as ( 
    SELECT RN, DTE, NM
    FROM   CalendarTable
    where  rn = @rn and 
           nm = @Day
    )

,Counts
as (
    Select  pp.id, Count(pp.id) as cnt
    from    PayRoll.dbo.Punches as P
    join    PayRoll.dbo.People  as pp   on p.BadgeNo = pp.WASP_ID 
    join    TD                          on p.PunchDate = td.DTE
    where   pp.id = @peopleId
    and     @wk = (Case when PunchDate < @wk2Start 
                                    then 1 else 2
                                End)
    group by pp.id
)
,wkBreakOut
as (
    Select  td.NM, c.cnt, PunchActualTime, Case when PunchDate < @wk2Start 
                                    then 1 else 2
                                End as wk,
            pp.id, pp.name, p.PunchDate, p.PunchTime, p.Notes,
            row_number() over (partition by PunchDate 
                                order by PunchDate, PunchActualTime) as rn
    from    PayRoll.dbo.Punches as P
    join    PayRoll.dbo.People  as pp on p.BadgeNo = pp.WASP_ID 
    join    TD                          on p.PunchDate = td.DTE
    join    Counts as c on pp.id = c.id
    where   pp.id = @peopleId
    )

 ,OutTimes
  as (
  select    t.* , case when t2.rn % 2 = 0 then  t2.PunchActualTime end as OutTime
  from      wkBreakOut as t 
  left join wkBreakOut as t2 on t.PunchDate = t2.PunchDate and 
                                t.rn = t2.rn - 1
                                    )

,TimeCalcs
as 
(
Select  Name, punchDate, td.nm,
        PunchActualTime as InTime, 
        OutTime, DateDiff(mi, PunchActualTime, OutTime ) as duration,
        Notes
from    OutTimes AS OT
join    TD                      on ot.PunchDate = td.DTE
where   --OutTime IS NOT NULL

        @wk = (Case when PunchDate < @wk2Start 
                        then 1 else 2
                    End)
)

,PunchesWithDurations
as
(
    Select  tc.*,
            left(cast(InTime as time),5) as PunchTime,
            Rtrim(Cast(duration / 60 as char(2))) + '.' 
            + substring( Cast(Round((duration % 60 ) / 60.0,2) as char(10)),3,2)
               as HrsAndMins, 
            rn = row_number() over ( partition by punchDate order by InTime )
    from    TimeCalcs as tc
)
--convert(varchar,@timeValue,108);

,result
as 

(

Select  p.name, p.NM, p.PunchTime, p.Intime,

        --p2.HrsAndMins as HrsAndMinsD,
        REPLACE(CONVERT(CHAR(5),Left(DATEADD(mi, (p2.HrsAndMins - FLOOR(p2.HrsAndMins)) * 60, DATEADD(hh, FLOOR(p2.HrsAndMins), 
                CAST ('00:00:00' AS TIME))),5)),':','-') as HrsAndMins,
        p.Notes
from    PunchesWithDurations as p
left join   PunchesWithDurations as p2 on p.rn = p2.rn + 1

)

Select name, NM, PunchTime, HrsAndMins, Notes
from result

order by InTime

--Select    wk, name, NM, PunchDate, PunchTime, Notes
--from  wkBreakOut
--where wk = @wk
--ORDER BY PunchTime

--      Exec CrossTabDetail_Dev @rn = 9, @peopleId = 11, @wk = 1, @Day = 'Monday' 
--      Exec CrossTabDetail @rn = 9, @peopleId = 17, @wk = 1, @Day = 'Monday' 

--      Exec CrossTabDetail_v3 @rn = 9, @peopleId = 17, @wk = 1, @Day = 'Monday' 

0 个答案:

没有答案