PHP-MSSQL - 使用MSSQL数据库在PHP中运行复杂的SQL脚本

时间:2013-12-11 11:09:21

标签: php sql-server

我正在尝试运行一个在SQL Manager中运行良好的相当复杂的SQL脚本。 经过一些障碍让我的php服务器连接到MSSQL后,简单的脚本(选择)工作,但是我试图重新使用的这个脚本什么都不返回。

脚本如下:

--=====================================================================================================================
-- Step 1.9.10: Cleanup existing temporary tables.
--=====================================================================================================================
if exists ( select * from tempdb..sysobjects    where   id = object_id('tempdb..#CU_RPT_PJ', 'U'))
begin
    Drop Table #CU_RPT_PJ
end


--=====================================================================================================================
select
    -- The resulting non-pivoted columns.
        Em_M_Id             = pvt.Em_M_Id                       
    ,   EmCd                        = pvt.EmCd                      
    ,   DisplayName                 = pvt.DisplayName               
    ,   PCal_ClsDate                = pvt.PCal_ClsDate
    ,   PayCalcBatch_Id             = pvt.PayCalcBatch_Id
    ,   PCalendar_Id                = pvt.PCalendar_Id
    -- The Link Table data from : [ HM_EmpDept_Link ].
    ,   OrgNode_Code                    = cast(Null AS nvarchar(50))
    ,   OrgNode_SName                   = cast(Null AS nvarchar(50))
    ,   OrgNode_LName                   = cast(Null AS nvarchar(80))
    ,   OrgNode_CostCode                = cast(Null AS nvarchar(50))
    ,   TreePath_SDesc                  = cast(Null AS nvarchar(255))
    ,   TreePath_LDesc                  = cast(Null AS nvarchar(255))

-- The resulting pivoted columns.
,[catc - Unit]                  = pvt.[01]
,[catc - Amt]                   = pvt.[02]
,[MlAllo - Amt]                 = pvt.[03]
,[BaSalCat - Unit]  = pvt.[04]
,[BaSalCat - Amt]   = pvt.[05]
,[Otr 1.5 - Unit]           = pvt.[06]
,[Otr 1.5 - Amt]            = pvt.[07]
,[Otr 2.0 - Unit]           = pvt.[08]
,[Otr 2.0 - Amt]            = pvt.[09]
,[Otr 3.0 - Unit]           = pvt.[10]
,[Otr 3.0 - Amt]            = pvt.[11]
,[Ilv - Unit]                   = pvt.[12]
,[Ilv - Amt]                    = pvt.[13]
,[Slv - Unit]                   = pvt.[14]
,[Slv - Amt]                        = pvt.[15]
,[Llv - Unit]                   = pvt.[16]
,[Llv - Amt]                    = pvt.[17]
,[Plv - Unit]               = pvt.[18]
,[Plv - Amt]                = pvt.[19]
,[Mlv - Unit]               = pvt.[20]
,[Mlv - Amt]                = pvt.[21]
,[Wdg - Unit]                       = pvt.[22]
,[Wlv - Amt]                    = pvt.[23]
,[BFn - Amt]                = pvt.[24]
,[AtB - Amt]                = pvt.[25]
,[RdfP - Amt]               = pvt.[26]
,[RdHP - Amt]               = pvt.[27]
,[Adj - Amt]                        = pvt.[28]
,[TEarn - Amt]                  = pvt.[29]
,[Nem - Amt]                    = pvt.[30]
,[NsE - Amt]                    = pvt.[31]
,[TEmo Curr - Amt]          = pvt.[32]
,[BfA - Amt]                = pvt.[33]
,[AbF - Amt]            = pvt.[34]
,[Lded - Amt]                   = pvt.[35]
,[MfpCon - Amt]             = pvt.[36]
,[Npy - Amt]                        = pvt.[37]                  
,[UdAm - Amt]           = pvt.[38]  
,[CunCon - Amt]     = pvt.[39]  
,[IncTxPc   - Amt]      = pvt.[40]  
,[ULv]                  = pvt.[41]  
,[UpSck]                            = pvt.[42]  
,[IunpLv]                   = pvt.[43]  
,[SPdLv - Unit]         = pvt.[44]  
,[SPdLv - Amt]              = pvt.[45]  
,[SUPdLv]                   = pvt.[46]  
,[VcUnpLv]              = pvt.[47]  
,[BnLH - Amt]               = pvt.[48]
,[VcPdLv - Unit]            = pvt.[49]  
,[VcPdLv - Amt]         = pvt.[50]  
,[SvAl]                 = pvt.[51]  
,[SsPdLv]               = pvt.[52]  
,[EyBnC]                        = pvt.[53]  
,[RLlv]                 = pvt.[54]  
,[RSLv]                 = pvt.[55]  


into    #CU_RPT_PJ
from    (
        select  
            -- These columns used for as non-pivoted columns.
                pct.Em_M_Id
            ,   pct.EmCd
            ,   pct.DisplayName
            ,   pct.PCal_ClsDate
            ,   pct.PayCalcBatch_Id
            ,   pct.PCalendar_Id
        --  ,   pct.PayCalcStep_Code
        --  ,   pct.PayCalcStep_Seqn
        --  ,   pct.PayCalcTrn_Unit
        --  ,   pct.PayCalcTrn_Amount
        --  ,   Alias_TagName           = sta.Alias_TagName
        --  ,   ValType                 = sta.ValType
        --  ,   sta_PayCalcStep_Code    = sta.PayCalcStep_Code

            -- These columns used for pivoting rows into columns.
            ,   QrySeqn                 = sta.QrySeqn
            ,   Common_Value            = case  sta.ValType     
                                                when    'U' then    pct.PayCalcTrn_Unit
                                                when    'A' then    pct.PayCalcTrn_Amount
                                                else                0.00
                                            end
        from    dbo.vHP_Data_PayCalcTrn_EmpList as pct

        join    (
                    select  Alias_TagName   = 'catc - Unit'                 , ValType   = 'U'   , QrySeqn   = '01'  , PayCalcStep_Code  = 'catc'                    
            union   select  Alias_TagName   = 'catc - Amt'                      , ValType   = 'A'   , QrySeqn   = '02'  , PayCalcStep_Code  = 'catc'                    
            union   select  Alias_TagName   = 'MlAllo - Amt'                    , ValType   = 'A'   , QrySeqn   = '03'  , PayCalcStep_Code  = 'MlAllo'  
            union   select  Alias_TagName   = 'BaSalCat - Unit'     , ValType   = 'U'   , QrySeqn   = '04'  , PayCalcStep_Code  = 'BaSalCat'    
            union   select  Alias_TagName   = 'BaSalCat - Amt'      , ValType   = 'A'   , QrySeqn   = '05'  , PayCalcStep_Code  = 'BaSalCat'    
            union   select  Alias_TagName   = 'Otr 1.5 - Unit'              , ValType   = 'U'   , QrySeqn   = '06'  , PayCalcStep_Code  = 'Otr 1.5' 
            union   select  Alias_TagName   = 'Otr 1.5 - Amt'               , ValType   = 'A'   , QrySeqn   = '07'  , PayCalcStep_Code  = 'Otr 1.5' 
            union   select  Alias_TagName   = 'Otr 2.0 - Unit'              , ValType   = 'U'   , QrySeqn   = '08'  , PayCalcStep_Code  = 'Otr 2.0' 
            union   select  Alias_TagName   = 'Otr 2.0 - Amt'               , ValType   = 'A'   , QrySeqn   = '09'  , PayCalcStep_Code  = 'Otr 2.0' 
            union   select  Alias_TagName   = 'Otr 3.0 - Unit'              , ValType   = 'U'   , QrySeqn   = '10'  , PayCalcStep_Code  = 'Otr 3.0' 
            union   select  Alias_TagName   = 'Otr 3.0 - Amt'               , ValType   = 'A'   , QrySeqn   = '11'  , PayCalcStep_Code  = 'Otr 3.0' 
            union   select  Alias_TagName   = 'Ilv - Unit'                      , ValType   = 'U'   , QrySeqn   = '12'  , PayCalcStep_Code  = 'Ilv'                     
            union   select  Alias_TagName   = 'Ilv - Amt'                       , ValType   = 'A'   , QrySeqn   = '13'  , PayCalcStep_Code  = 'Ilv'                     
            union   select  Alias_TagName   = 'Slv - Unit'                      , ValType   = 'U'   , QrySeqn   = '14'  , PayCalcStep_Code  = 'Slv'                     
            union   select  Alias_TagName   = 'Slv - Amt'                       , ValType   = 'A'   , QrySeqn   = '15'  , PayCalcStep_Code  = 'Slv'                     
            union   select  Alias_TagName   = 'Llv - Unit'                      , ValType   = 'U'   , QrySeqn   = '16'  , PayCalcStep_Code  = 'Llv'                     
            union   select  Alias_TagName   = 'Llv - Amt'                       , ValType   = 'A'   , QrySeqn   = '17'  , PayCalcStep_Code  = 'Llv'                     
            union   select  Alias_TagName   = 'Plv - Unit'                  , ValType   = 'U'   , QrySeqn   = '18'  , PayCalcStep_Code  = 'Plv'                     
            union   select  Alias_TagName   = 'Plv - Amt'                   , ValType   = 'A'   , QrySeqn   = '19'  , PayCalcStep_Code  = 'Plv'                     
            union   select  Alias_TagName   = 'Mlv - Unit'                  , ValType   = 'U'   , QrySeqn   = '20'  , PayCalcStep_Code  = 'Mlv'                     
            union   select  Alias_TagName   = 'Mlv - Amt'                   , ValType   = 'A'   , QrySeqn   = '21'  , PayCalcStep_Code  = 'Mlv'                     
            union   select  Alias_TagName   = 'Wdg - Unit'                          , ValType   = 'U'   , QrySeqn   = '22'  , PayCalcStep_Code  = 'Wlv'                     
            union   select  Alias_TagName   = 'Wlv - Amt'                       , ValType   = 'A'   , QrySeqn   = '23'  , PayCalcStep_Code  = 'Wlv'                     
            union   select  Alias_TagName   = 'BFn - Amt'                   , ValType   = 'A'   , QrySeqn   = '24'  , PayCalcStep_Code  = 'BFn' 
            union   select  Alias_TagName   = 'AtB - Amt'                   , ValType   = 'A'   , QrySeqn   = '25'  , PayCalcStep_Code  = 'AtB' 
            union   select  Alias_TagName   = 'RdfP - Amt'              , ValType   = 'A'   , QrySeqn   = '26'  , PayCalcStep_Code  = 'RdfP'                        
            union   select  Alias_TagName   = 'RdHP - Amt'              , ValType   = 'A'   , QrySeqn   = '27'  , PayCalcStep_Code  = 'RdHP'                        
            union   select  Alias_TagName   = 'Adj - Amt'                       , ValType   = 'A'   , QrySeqn   = '28'  , PayCalcStep_Code  = 'Allowance'                       
            union   select  Alias_TagName   = 'TEarn - Amt'                 , ValType   = 'A'   , QrySeqn   = '29'  , PayCalcStep_Code  = 'TEarn'   
            union   select  Alias_TagName   = 'Nem - Amt'                       , ValType   = 'A'   , QrySeqn   = '30'  , PayCalcStep_Code  = 'Nem' 
            union   select  Alias_TagName   = 'NsE - Amt'                       , ValType   = 'A'   , QrySeqn   = '31'  , PayCalcStep_Code  = 'NsE' 
            union   select  Alias_TagName   = 'TEmo Curr - Amt'             , ValType   = 'A'   , QrySeqn   = '32'  , PayCalcStep_Code  = 'TEmo Curr'   
            union   select  Alias_TagName   = 'BfA - Amt'                   , ValType   = 'A'   , QrySeqn   = '33'  , PayCalcStep_Code  = 'BfA' 
            union   select  Alias_TagName   = 'AbF - Amt'               , ValType   = 'A'   , QrySeqn   = '34'  , PayCalcStep_Code  = 'AbF'                     
            union   select  Alias_TagName   = 'Lded - Amt'                  , ValType   = 'A'   , QrySeqn   = '35'  , PayCalcStep_Code  = 'Lded'    
            union   select  Alias_TagName   = 'MfpCon - Amt'                , ValType   = 'A'   , QrySeqn   = '36'  , PayCalcStep_Code  = 'MfpCon'  
            union   select  Alias_TagName   = 'Npy - Amt'                           , ValType   = 'A'   , QrySeqn   = '37'  , PayCalcStep_Code  = 'Npy' 
            union   select  Alias_TagName   = 'UdAm - Amt'          , ValType   = 'A'   , QrySeqn   = '38'  , PayCalcStep_Code  = 'UdAm'    
            union   select  Alias_TagName   = 'CunCon - Amt'            , ValType   = 'A'   , QrySeqn   = '39'  , PayCalcStep_Code  = 'CunCon'
            union   select  Alias_TagName   = 'IncTxPc - Amt'           , ValType   = 'A'   , QrySeqn   = '40'  , PayCalcStep_Code  = 'IncTxPc'
            union   select  Alias_TagName   = 'ULv'                     , ValType   = 'U'   , QrySeqn   = '41'  , PayCalcStep_Code  = 'ULv'         
            union   select  Alias_TagName   = 'UpSck'                               , ValType   = 'U'   , QrySeqn   = '42'  , PayCalcStep_Code  = 'UpSck'           
            union   select  Alias_TagName   = 'IunpLv'                      , ValType   = 'U'   , QrySeqn   = '43'  , PayCalcStep_Code  = 'IunpLv'          
            union   select  Alias_TagName   = 'SPdLv - Unit'                , ValType   = 'U'   , QrySeqn   = '44'  , PayCalcStep_Code  = 'SPdLv'   
            union   select  Alias_TagName   = 'SPdLv'                       , ValType   = 'A'   , QrySeqn   = '45'  , PayCalcStep_Code  = 'SPdLv'   
            union   select  Alias_TagName   = 'SUPdLv'                  , ValType   = 'U'   , QrySeqn   = '46'  , PayCalcStep_Code  = 'SUPdLv'
            union   select  Alias_TagName   = 'VcUnpLv'                 , ValType   = 'U'   , QrySeqn   = '47'  , PayCalcStep_Code  = 'VcUnpLv'
            union   select  Alias_TagName   = 'BnLH - Amt'                  , ValType   = 'A'   , QrySeqn   = '48'  , PayCalcStep_Code  = 'BnLH Amount' 
            union   select  Alias_TagName   = 'VcPdLv'                      , ValType   = 'U'   , QrySeqn   = '49'  , PayCalcStep_Code  = 'VcPdLv'
            union   select  Alias_TagName   = 'VcPdLv'                      , ValType   = 'A'   , QrySeqn   = '50'  , PayCalcStep_Code  = 'VcPdLv'
            union   select  Alias_TagName   = 'SvAl'                        , ValType   = 'A'   , QrySeqn   = '51'  , PayCalcStep_Code  = 'SvAl'
            union   select  Alias_TagName   = 'SsPdLv'                  , ValType   = 'A'   , QrySeqn   = '52'  , PayCalcStep_Code  = 'SsPdLv'
            union   select  Alias_TagName   = 'EyBnC'                           , ValType   = 'A'   , QrySeqn   = '53'  , PayCalcStep_Code  = 'EyBnC'
            union   select  Alias_TagName   = 'RLlv'                        , ValType   = 'A'   , QrySeqn   = '54'  , PayCalcStep_Code  = 'RLlv'
            union   select  Alias_TagName   = 'RSLv'                        , ValType   = 'A'   , QrySeqn   = '55'  , PayCalcStep_Code  = 'RSLv'

            )   as sta
            on  pct.PayCalcStep_Code    = sta.PayCalcStep_Code

        where   pct.Em_M_Id > 0

            and pct.PayCalcBatch_Id             in ( 

select  pcb.PayCalcBatch_Id 
from    dbo.fHP_Log_PayCalcBatch (0, 1, '', 1, '', 1, '')   as pcb
where   pcb.PayCalcBatch_Id > 0
    and pcb.PayCalGroup_LName = 'Something-1'
    and pcb.PayCalcBatch_Status = 30
    and pcb.PayCalc_SttDate between '2013-01-01' and '2013-12-10'


        )

        )   as pct
    pivot   (
        sum(pct.Common_Value)
    for pct.QrySeqn in (
            [01]
        ,   [02]
        ,   [03]
        ,   [04]
        ,   [05]
        ,   [06]
        ,   [07]
        ,   [08]
        ,   [09]
        ,   [10]
        ,   [11]
        ,   [12]
        ,   [13]
        ,   [14]
        ,   [15]
        ,   [16]
        ,   [17]
        ,   [18]
        ,   [19]
        ,   [20]
        ,   [21]
        ,   [22]
        ,   [23]
        ,   [24]
        ,   [25]
        ,   [26]
        ,   [27]
        ,   [28]
        ,   [29]
        ,   [30]
        ,   [31]
        ,   [32]
        ,   [33]
        ,   [34]
        ,   [35]
        ,   [36]
        ,   [37]
        ,   [38]
        ,   [39]
        ,   [40]
        ,   [41]
        ,   [42]
        ,   [43]
        ,   [44]
        ,   [45]
        ,   [46]
        ,   [47]
        ,   [48]
        ,   [49]
        ,   [50]
        ,   [51]
        ,   [52]
        ,   [53]
        ,   [54]
        ,   [55]
        )    
    )   as pvt

--=====================================================================================================================
-- Step 3.5.21: Update the Dataset with Employee-OrgChart Link Data.
--=====================================================================================================================


update  rpt
    set
        OrgNode_Code                    = edl.OrgNode_Code  
    ,   OrgNode_SName                   = edl.OrgNode_SName 
    ,   OrgNode_LName                   = edl.OrgNode_LName 
    ,   OrgNode_CostCode                = edl.OrgNode_CostCode
    ,   TreePath_SDesc                  = edl.TreePath_SDesc    
    ,   TreePath_LDesc                  = edl.TreePath_LDesc    

from    #CU_RPT_PJ          as rpt

inner join  dbo.vHM_EmpDept_Link            as edl
    on  rpt.Em_M_Id     = edl.Em_M_Id
    and rpt.PCal_ClsDate        between edl.Edl_Date_Joined and edl.Edl_Date_Left_Siml



--=====================================================================================================================
-- Step 4.20 : Return the Datasets.
--=====================================================================================================================
select  rpt.EmCd, rpt.DisplayName, rpt.PCal_ClsDate, rpt.PayCalcBatch_Id, rpt.[MlAllo - Amt], rpt.[BaSalCat - Amt], rpt.[Otr 1.5 - Amt], rpt.[Otr 2.0 - Amt], rpt.[Otr 3.0 - Amt], rpt.[Ilv - Amt], rpt.[Slv - Amt], rpt.[Llv - Amt], rpt.[Plv - Amt], rpt.[Mlv - Amt], rpt.[Wlv - Amt], rpt.[BFn - Amt], rpt.[AtB - Amt], rpt.[RdfP - Amt], rpt.[RdHP - Amt],rpt.[Adj - Amt], rpt.[TEarn - Amt], rpt.[Nem - Amt], rpt.[NsE - Amt], rpt.[TEmo Curr - Amt], rpt.[BfA - Amt], rpt.[AbF - Amt], rpt.[Lded - Amt], rpt.[MfpCon - Amt],rpt.[Npy - Amt], rpt.[UdAm - Amt], rpt.[CunCon - Amt], rpt.[IncTxPc   - Amt], rpt.[ULv], rpt.[UpSck], rpt.[IunpLv], rpt.[SPdLv - Amt], rpt.[SUPdLv], rpt.[VcPdLv - Amt], rpt.[BnLH - Amt], rpt.[VcPdLv - Amt], rpt.[SvAl], rpt.[SsPdLv], rpt.[EyBnC], rpt.[RLlv], rpt.[RSLv]
from    #CU_RPT_PJ  as rpt
where   rpt.EmCd = '12345'
order by PCal_ClsDate

使用的部分PHP代码(凭证省略)$ query =与上面的SQL相同。 检索方法可能是错误的。我一直在尝试一些,虽然我愿意接受建议:)

<?php

$connectionInfo = array("UID"=>$myUser, "PWD"=>$myPass, "Database"=>$myDB);

$dbhandle = sqlsrv_connect($myServer,$connectionInfo)
or die ( print_r( sqlsrv_errors(), true));



$query = "Same query as above verbatim"

//execute the SQL query and return records
$result = sqlsrv_query($dbhandle, $query) or die ( print_r( sqlsrv_errors(), true));


echo "Result \n";
//display the results
print_r($result);


while($row = sqlsrv_fetch_array($result) or die ( print_r( sqlsrv_errors(), true)))
{

  echo "<li>" . $row[0] ."-". $row[1] ."-". $row[2] . "</li>";


}


sqlsrv_close($dbhandle);
?>

的print_r($结果); //返回以下内容:

Result Resource id #3Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -28 [code] => -28 [2] => The active result for the query contains no fields. [message] => The active result for the query contains no fields. ) )

0 个答案:

没有答案