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 모드에서 잘못 처리하고있는 것이 확실합니다.
의견을 보내 주시면 대단히 감사하겠습니다.
덕분에, 스콧
데이터 세트에서 명령을 설정 했습니까? 데이터 소스에 액세스 할 수 있습니까? –