2013-11-25 93 views
0

我正在嘗試創建一個從SSRS引用變量輸入的存儲過程。這是複雜查詢的代碼。我正在使用CTE來使代碼更具可讀性。創建要在SSRS中使用的變量存儲過程

/****** Object: StoredProcedure [dbo].[adm_AuditHospMonth] Script Date: 11/25/2013 9:39:10 AM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

/* 
-- ============================================= 
-- Author:  Scott Schmeling 
-- Create date: 11/25/2013 
-- Description: Determines the products in which the price was lowered and revenue lost during a set time period. 
-- ============================================= 
*/ 


Create Procedure dbo.PriceErosion 
    @StartDate as Date 
    ,@EndDate as Date 
    ,@CurDate as Date 
    ,@Hospital as Int 
    ,@Division as Int 

as 

/* 
Test Data 

Declare @StartDate as Date 
Declare @EndDate as Date 
Declare @Hospital as Int 
Declare @Division as Int 
DECLARE @curDate Date 

SET @curDate = GETDATE() 
Set @StartDate = CASE WHEN @StartDate IS NULL THEN DATEADD(dd, -31, Dateadd(dd, -1, @curdate)) ELSE @StartDate END 
Set @EndDate = CASE WHEN @EndDate IS NULL THEN Dateadd(dd, -1, @curdate) ELSE @EndDate END 
Set @Hospital = 3; 
*/ 

Begin 

    -- Sets the Baseline Price Date in the PriceChangeHistory Table. 
    With PC1 
    as 
     (Select 
      HospitalMasterID 
      ,TxnCode 
      ,UserInfoMasterID 
      ,Active 
      ,min(TxnDateTime) as StartingDate 
     From 
     PriceChangeHistory 
     Where 
     TxnDateTime Between @StartDate and @EndDate 
     Group By 
      HospitalMasterID, TxnCode, UserInfoMasterID, Active) 

    -- Gets the Baseline Price for the period from the PriceChangeHistory Table 
    ,PC 
    as 
     (Select 
      PC1.HospitalMasterID 
      ,PC1.TxnCode 
      ,PC1.UserInfoMasterID 
      ,PC1.Active 
      ,Cast (PC1.StartingDate as Date) as StartingDate 
      ,PC2.OldPrice as StartingPrice 
      ,PC2.NewPrice 
      ,PC2.TxnSubType 
     From 
     PC1 
     Inner Join 
     PriceChangeHistory as PC2 
     On 
     PC1.HospitalMasterID = PC2.HospitalMasterID 
     and 
     PC1.TxnCode = PC2.TxnCode 
     and 
     PC1.StartingDate = PC2.TxnDateTime 
     Where 
     PC2.OldPrice > PC2.NewPrice) 

    --MedicalHistory Information 
    ,MH 
    as 
     (Select 
      HospitalMasterID 
      ,PatientID 
      ,TxnDate 
      ,TxnCode 
      ,Description 
      ,ListAmount 
      ,ExtendedAmount 
      ,TxnType 
      ,Quantity 
      ,(Case 
       When Quantity <> '1' Then (ListAmount/Quantity) 
       Else ListAmount 
       End) as UnitPrice 
     From 
      MedicalHistory 
     Where 
      TxnDate Between @StartDate and @EndDate 
      and 
      _IsServOrITem = 1) 

    -- Determines the Revenue lost per each sale, also reduces the results to only those items where the Price was lowered not raised. 
    ,RL 
    as 
     (Select 
      PC.HospitalMasterID 
      ,MH.PatientID 
      ,PC.TxnCode 
      ,PC.TxnSubType 
      ,MH.Description 
      ,PC.UserInfoMasterID as ChangedByUserID 
      ,MH.TxnDate 
      ,PC.StartingPrice 
      ,Cast (MH.UnitPrice as Money) as UnitPrice 
      ,Cast ((StartingPrice - UnitPrice) as Money) as RevenueLost 
     From 
     PC 
     Left OUter Join 
     MH 
     on 
     PC.HospitalMasterID = MH.HospitalMasterID 
     and 
     PC.TxnCode = MH.TxnCode 
     Where 
     PC.StartingPrice > MH.UnitPrice) 

    --- Determine the name of the tech changing the prices. 
    ,UI 
    as 
     (Select 
      HospitalMasterID 
      ,UserInfoMasterID 
      ,Name 
     From 
      UserInfo) 

    --- Get the Division and Hospital Name for each Hospital. 

    ,HODI 
    as 
     (Select 
      DI.DivisionID 
      ,DI.DivisionName 
      ,HO.HospMastID 
      ,HO.HospCode 
      ,HO.HospName 
     From 
      ref_Hospital as HO 
      inner Join 
      ref_Division as DI 
      on 
      HO.DivisionID = DI.DivisionID) 

    ,HI 
    as 
     (Select 
      HODI.DivisionID 
      ,HODI.DivisionName 
      ,RL.HospitalMasterID 
      ,HODI.HospCode 
      ,HODI.HospName 
      ,RL.PatientID 
      ,RL.TxnCode 
      ,RL.TxnSubType 
      ,RL.Description 
      ,RL.ChangedByUserID 
      ,RL.TxnDate 
      ,RL.StartingPrice 
      ,RL.UnitPrice 
      ,RL.RevenueLost 
     From 
      RL 
      Left Outer Join 
      HODI 
      ON 
      RL.HospitalMasterID = HODI.HospMastID 
      Where 
      RL.HospitalMasterID = @Hospital 
      and 
      RL.DivisionID = @Division 
      and 
      TXNDate Between @StartDate and @EndDate) 

Select 
* 
From 
HI 

End 

每次嘗試通過SSRS運行此存儲過程時,都會收到一條錯誤消息,指出未定義變量。我確信在SP模式下我做了一些不正確的事情,因爲查詢和測試數據本身都可以正常工作。

任何建議將不勝感激。

感謝, 斯科特

+0

您是否在數據集中設置了命令?數據源是否可訪問? –

回答

1

SSRS應該能夠探測到什麼參數的存儲過程需求,並自動添加它們。它不幸地不足以處理數據類型,所以你將不得不手動選擇這些數據類型。

選擇一個新的數據集並選擇存儲過程。使。確保您選擇完全限定的名稱。然後點擊刷新字段按鈕。

Sproc

如果檢查數據集的參數標籤,你應該看到您的參數已經添加,如果沒有,那麼你可以手動添加。請記住,參數名稱區分大小寫。

Sproc2

最後,你必須進入每個參數的屬性和手動選擇正確的數據類型爲SSRS將默認爲文本。只需雙擊屏幕左側的報告數據中的參數即可。

Sproc1

NB。你似乎沒有在你的sproc中的任何地方使用CurDate,所以你可以刪除它。

+0

謝謝,它運作良好。 CurDate用於Case StartDate和Case EndDate爲空。這可能不是最好的辦法。如果有更好的,我會很樂意使用它。 – SASUSMC

+0

@SASUSMC在使用GETDATE()設置存儲區中的值時,不需要將其傳遞到存儲區。你也可以使用sql ['ISNULL'](http://technet.microsoft.com/en-us/library/ms184325.aspx)函數而不是CASE WHEN。 – Sam