如何将导入的SQL查询正确转换为SSRS中的表达式字符串

问题描述 投票:0回答:1

很抱歉打扰每个人,这是一个微不足道的问题,但是我需要将一个SQL查询导入SSRS的“报告”构建器,然后将其转换为表达式(以便我可以使用其串联运算符将参数值连接到搜索字符串)。

[在第一阶段,我将查询(我知道有效)复制并粘贴到SSRS中,并且运行顺利。但是,只要将表达式放在引号中,并在整个内容前加上“ =”,我就会收到一些非常无用的语法错误消息。

这是我(运行正常)的查询,然后导入到SSRS:

DECLARE @From Date='1/1/2015'
DECLARE @To Date='1/3/2020'
DECLARE @Doctors varchar(5)='ABC'
DECLARE @Intents varchar(20)='RoutineRad-Cat2'
DECLARE @DiagnosisTypes varchar(20)='ICD-10'

SET transaction isolation level read uncommitted;

SELECT
    x.Expression1 as 'Intent',
    x.IntentCategory,
    x.StartDateTime as 'Ready To Start Treatment Date',
    x.LastName as 'Surname',
    x.PatientId as 'Patient ID',
    x.DoctorId as 'Primary Oncologist',
    x.TreatmentStartTime as 'First RT Treatment Date',
    datediff(dd, x.StartDateTime, x.TreatmentStartTime) as 'Wait in days',
    x.TargetMet,
    x.Delayed,
    x.TxStartSortDate

FROM (VALUES('C50%'),('D05%'),('C61%'))

AS v (pattern)
  CROSS APPLY
  (
    SELECT 
            lut.Expression1,
            p.PatientId, 
            p.LastName,
            c.StartDateTime,
            d.DoctorId,
            rh.TreatmentStartTime,
            CASE    
                WHEN Expression1 like '%Rad%' THEN 'Radical'
                WHEN Expression1 like '%Pall%' THEN 'Palliative'
                WHEN Expression1 = 'Emergency' THEN 'Emergency'
            ELSE 'Other'
            END
            as 'IntentCategory',

            CASE WHEN Expression1 like '%Delay%' THEN 1 ELSE 0 END as 'Delayed',
            CASE WHEN 
            (
                (Expression1 like '%Rad%' AND datediff(dd, c.StartDateTime, FirstRadiationHstry.TreatmentStartTime) <= 28)  
                OR (Expression1 like '%Palliative%' AND datediff(dd, c.StartDateTime, FirstRadiationHstry.TreatmentStartTime) <= 14)
                OR (Expression1 = 'Emergency' AND datediff(dd, c.StartDateTime, FirstRadiationHstry.TreatmentStartTime) <= 1)
                --catch any exceptions
                OR ((Expression1 not like '%Rad%') AND (Expression1 not like '%Palliative%') AND (Expression1!='Emergency'))
            ) THEN 1 ELSE 0 END as 'TargetMet',
            FORMAT(rh.TreatmentStartTime,'yyyyMM') as 'TxStartSortDate',
            FirstRadiationHstry.TreatmentStartTime as 'MinTreatStart',
            datediff(dd, c.StartDateTime, rh.TreatmentStartTime) as 'TreatDelay'

            FROM
            dbo.Patient p
            inner join Course c on (c.PatientSer = p.PatientSer)
            inner join CourseDiagnosis cd on (cd.CourseSer=c.CourseSer)
            inner join Diagnosis diag on (diag.DiagnosisSer=cd.DiagnosisSer)
            inner join PatientDoctor pd on (pd.PatientSer = p.PatientSer)
            inner join Doctor d on (d.ResourceSer = pd.ResourceSer)
            inner join dbo.PhysicianIntent phi on (phi.CourseSer = c.CourseSer)
            inner join dbo.LookupTable lut on (lut.LookupValue = phi.TreatmentIntentType and lut.ListSelector = 'COURSE_INTENT')
            inner join dbo.PlanSetup ps on (ps.CourseSer = c.CourseSer)
            inner join dbo.Radiation r on (r.PlanSetupSer = ps.PlanSetupSer)
            inner join dbo.RadiationHstry rh on (rh.RadiationSer = r.RadiationSer)
            inner join (
                            select distinct
                            min(rh_sub.TreatmentStartTime) over (partition by ps_sub.CourseSer) as 'TreatmentStartTime',
                            ps_sub.CourseSer
                            from
                            RadiationHstry rh_sub 
                            inner join dbo.Radiation r_sub on (r_sub.RadiationSer = rh_sub.RadiationSer)
                            inner join dbo.PlanSetup ps_sub on (r_sub.PlanSetupSer = ps_sub.PlanSetupSer)
                            where
                            rh_sub.TreatmentDeliveryType like 'TREATMENT'
                        ) as FirstRadiationHstry on (FirstRadiationHstry.CourseSer = c.CourseSer)


    WHERE       
            datediff(dd, @From, rh.TreatmentStartTime) >= 0
            and datediff(dd, rh.TreatmentStartTime, @To)>=0
            and diag.DiagnosisCode like v.pattern
            and d.OncologistFlag=1
            and d.DoctorId in (@Doctors)
            and rh.TreatmentStartTime = FirstRadiationHstry.TreatmentStartTime
            and lut.Expression1 in (@Intents)
            and diag.DiagnosisTableName in(@DiagnosisTypes)
            and lut.Expression1 not like 'Dummy Course'
  ) AS x
;

...这是它的后面:

="SET transaction isolation level read uncommitted;

SELECT
    x.Expression1 as 'Intent',
    x.IntentCategory,
    x.StartDateTime as 'Ready To Start Treatment Date',
    x.LastName as 'Surname',
    x.PatientId as 'Patient ID',
    x.DoctorId as 'Primary Oncologist',
    x.TreatmentStartTime as 'First RT Treatment Date',
    datediff(dd, x.StartDateTime, x.TreatmentStartTime) as 'Wait in days',
    x.TargetMet,
    x.Delayed,
    x.TxStartSortDate

FROM (VALUES('C50%'),('D05%'),('C61%'))

AS v (pattern)
  CROSS APPLY
  (
    SELECT 
            lut.Expression1,
            p.PatientId, 
            p.LastName,
            c.StartDateTime,
            d.DoctorId,
            rh.TreatmentStartTime,
            CASE    
                WHEN Expression1 like '%Rad%' THEN 'Radical'
                WHEN Expression1 like '%Pall%' THEN 'Palliative'
                WHEN Expression1 = 'Emergency' THEN 'Emergency'
            ELSE 'Other'
            END
            as 'IntentCategory',

            CASE WHEN Expression1 like '%Delay%' THEN 1 ELSE 0 END as 'Delayed',
            CASE WHEN 
            (
                (Expression1 like '%Rad%' AND datediff(dd, c.StartDateTime, FirstRadiationHstry.TreatmentStartTime) <= 28)  
                OR (Expression1 like '%Palliative%' AND datediff(dd, c.StartDateTime, FirstRadiationHstry.TreatmentStartTime) <= 14)
                OR (Expression1 = 'Emergency' AND datediff(dd, c.StartDateTime, FirstRadiationHstry.TreatmentStartTime) <= 1)
                --catch any exceptions
                OR ((Expression1 not like '%Rad%') AND (Expression1 not like '%Palliative%') AND (Expression1!='Emergency'))
            ) THEN 1 ELSE 0 END as 'TargetMet',
            FORMAT(rh.TreatmentStartTime,'yyyyMM') as 'TxStartSortDate',
            FirstRadiationHstry.TreatmentStartTime as 'MinTreatStart',
            datediff(dd, c.StartDateTime, rh.TreatmentStartTime) as 'TreatDelay'

            FROM
            dbo.Patient p
            inner join Course c on (c.PatientSer = p.PatientSer)
            inner join CourseDiagnosis cd on (cd.CourseSer=c.CourseSer)
            inner join Diagnosis diag on (diag.DiagnosisSer=cd.DiagnosisSer)
            inner join PatientDoctor pd on (pd.PatientSer = p.PatientSer)
            inner join Doctor d on (d.ResourceSer = pd.ResourceSer)
            inner join dbo.PhysicianIntent phi on (phi.CourseSer = c.CourseSer)
            inner join dbo.LookupTable lut on (lut.LookupValue = phi.TreatmentIntentType and lut.ListSelector = 'COURSE_INTENT')
            inner join dbo.PlanSetup ps on (ps.CourseSer = c.CourseSer)
            inner join dbo.Radiation r on (r.PlanSetupSer = ps.PlanSetupSer)
            inner join dbo.RadiationHstry rh on (rh.RadiationSer = r.RadiationSer)
            inner join (
                            select distinct
                            min(rh_sub.TreatmentStartTime) over (partition by ps_sub.CourseSer) as 'TreatmentStartTime',
                            ps_sub.CourseSer
                            from
                            RadiationHstry rh_sub 
                            inner join dbo.Radiation r_sub on (r_sub.RadiationSer = rh_sub.RadiationSer)
                            inner join dbo.PlanSetup ps_sub on (r_sub.PlanSetupSer = ps_sub.PlanSetupSer)
                            where
                            rh_sub.TreatmentDeliveryType like 'TREATMENT'
                        ) as FirstRadiationHstry on (FirstRadiationHstry.CourseSer = c.CourseSer)


    WHERE       
            datediff(dd, @From, rh.TreatmentStartTime) >= 0
            and datediff(dd, rh.TreatmentStartTime, @To)>=0
            and diag.DiagnosisCode like v.pattern
            and d.OncologistFlag=1
            and d.DoctorId in (@Doctors)
            and rh.TreatmentStartTime = FirstRadiationHstry.TreatmentStartTime
            and lut.Expression1 in (@Intents)
            and diag.DiagnosisTableName in(@DiagnosisTypes)
            and lut.Expression1 not like 'Dummy Course'
  ) AS x
;"

...但是我只是收到错误消息“关键字'VALUES'附近的语法不正确。')'附近的语法不正确。“

我不知道如何解决这个问题,因为我知道我的SQL很好。我尝试将查询导入到Scite中,并查看了行尾/特殊字符,但是看起来也不错。

有人可以帮我吗?

最诚挚的祝福

C J

sql-server reporting-services ssrs-2008
1个回答
0
投票
似乎您需要用引号将EACH LINE换行,并用

&链接以使此工作有效。我一直无法弄清楚为什么。我还使用换行符将结果查询中的行分开。

="SET transaction isolation level read uncommitted; " & VBCRLF " " & VBCRLF "SELECT " & VBCRLF " x.Expression1 as 'Intent', " & VBCRLF " x.IntentCategory, " & VBCRLF …
在Notepad ++中,您可以找到\r\n并替换为" & VBCRLF & \n"以自动添加它们-尽管您需要执行第一个和最后一个。

来源:找不到任何内容-反复试验。

© www.soinside.com 2019 - 2024. All rights reserved.