2016-10-31 75 views
0

這是我的存儲過程調用TEO列從tablar功能在SQL Server存儲過程:我怎麼能在同一時間

ALTER PROCEDURE [dbo].[PaymentStatusProviderDetailView] 
    (@POId INT , 
    @ProviderId INT , 
    @PrrId INT) 
AS 
BEGIN 
    SELECT 
     SDID, 
     dbo.GetServiceDetailString(SDID) AS ServiceName, 
     POID, 
     AmountPaid, AmountHeld, 
     ProviderName, 
     (SELECT ReimbursementAmount 
     FROM dbo.GetReimbursementBilledAmounts(@POId, SDID, @ProviderId, @PrrId)) AS ReimbursementAmount, 
     (SELECT ServiceMonth 
     FROM dbo.GetReimbursementBilledAmounts(@POId, SDID, @ProviderId, @PrrId)) AS ServiceMonth 
    FROM  
     (SELECT  
      SD.Id AS SDID, PO.Id AS POID, 
      SUM(PD.PaymentAmount) AS AmountPaid, 
      PH.HoldAmount AS AmountHeld, 
      PROV.ContractorName AS ProviderName 
     FROM  
      [dbo].[PaymentDetail] PD 
     JOIN 
      PurchaseOrder PO ON PO.Id = PD.PO_Id 
     JOIN 
      fBusinessUnit BU ON BU.id = PD.BU_Id 
     LEFT JOIN 
      Reimbursement_EBSUtilization REU ON REU.Id = PD.REU_Id 
     LEFT JOIN 
      Reimbursement_CDSUtilization RCU ON RCU.Id = PD.RCU_Id 
     LEFT JOIN 
      PaymentHold PH ON PH.PO_Id = PO.Id 
          AND (PH.RCU_Id = PD.RCU_Id OR PH.REU_Id = PD.REU_Id) 
     LEFT JOIN 
      ProviderReimbursementRequest PRR ON (PRR.Id = REU.PRR_Id OR PRR.Id = RCU.PRR_Id) 
     LEFT JOIN 
      fContractor PROV ON PROV.Id = PRR.Contractor_Id 
     LEFT JOIN 
      CDSUtilization CDS ON CDS.Id = RCU.CDSU_Id 
     LEFT JOIN 
      fServiceDetail SD ON SD.Id = REU.SD_Id OR SD.Id = CDS.ServiceDetail_Id 
     WHERE  
      Po.Id = @POId 
      AND PRR.Contractor_Id = @ProviderId 
      AND PRR.Id = @PrrId 
     GROUP BY 
      SD.Id, PO.Id, PH.HoldAmount, PROV.ContractorName 

     UNION 

     SELECT  
      SD.Id AS SDID, PO.Id AS POID, 
      NULL, 
      SUM(PH.HoldAmount) AS AmountHeld, 
      PROV.ContractorName AS ProviderName 
     FROM  
      PurchaseOrder PO 
     JOIN 
      PaymentHold PH ON PH.PO_Id = PO.Id 
     LEFT JOIN 
      Reimbursement_EBSUtilization REU ON REU.Id = PH.REU_Id 
     LEFT JOIN 
      Reimbursement_CDSUtilization RCU ON RCU.Id = PH.RCU_Id 
     LEFT JOIN 
      CDSUtilization CDS ON CDS.Id = RCU.CDSU_Id 
     JOIN 
      ProviderReimbursementRequest PRR ON PRR.Id = REU.PRR_Id OR RCU.PRR_Id = PRR.Id 
     JOIN 
      fContractor PROV ON PROV.Id = PRR.Contractor_Id 
     JOIN 
      fServiceDetail SD ON SD.Id = REU.SD_Id OR SD.Id = CDS.ServiceDetail_Id 
     WHERE  
      Po.Id = @POId 
      AND PRR.Contractor_Id = @ProviderId 
      AND PRR.Id = @PrrId 
      AND NOT EXISTS (SELECT 1 
          FROM PaymentDetail PD 
          WHERE PH.PO_Id = PD.PO_Id 
           AND (PH.RCU_Id = PD.RCU_Id OR PH.REU_Id = PD.REU_Id)) 
     GROUP BY 
      SD.Id, PO.Id, PROV.ContractorName) DT 
    END 

在上面的存儲過程,在特定的代碼我打電話了對於兩列

(SELECT ReimbursementAmount 
    FROM dbo.GetReimbursementBilledAmounts(@POId, SDID, @ProviderId, @PrrId)) AS ReimbursementAmount, 
(SELECT ServiceMonth 
    FROM dbo.GetReimbursementBilledAmounts(@POId, SDID, @ProviderId, @PrrId)) AS ServiceMonth 

我怎麼能說上面的功能在同一個存儲過程只有一次,在同一時間獲得兩個列名相同tablar功能兩次?

說,例如像下面

(SELECT ReimbursementAmount, ServiceMonth 
    FROM dbo.GetReimbursementBilledAmounts(@POId, SDID, @ProviderId, @PrrId)) AS ReimbursementAmount and ServiceMonth 

任何人都可以請幫我在這?

+1

簡答:你**不能**那樣做。 SELECT語句中的子查詢不能返回多個值。 –

+0

我以爲你可以在這種情況下使用'CROSS APPLY' –

回答

0

這看起來像在sql server中的限制。我們應該只使用列函數來檢索或顯示單個列。

0

這是行不通的嗎?

SELECT ....... 
........ 
RI.ReimbursementAmount, 
RI.ServiceMonth, 
... 

FROM 
..... 
.... 
CROSS APPLY dbo.GetReimbursementBilledAmounts(@POId, SDID, @ProviderId, @PrrId) RI 
    WHERE  
    Po.Id = @POId 
    AND PRR.Contractor_Id = @ProviderId 
    AND PRR.Id = @PrrId 
.... 
.. 
+0

IT確實有效,但問題是我仍然應該在聯合聲明中的兩個查詢中調用相同的函數兩次。 –

+0

我不明白你在說什麼 –