I’m running the following query in SSRS. If I add declarations for the two parameters, it runs fine in SQL Management Console.
declare @EMRQuery varchar(max)
declare @CPSQuery varchar(max)
set @EMRQuery = 'select Person.ExternalId
from ml.Person
join ml.Obs on Person.pId = Obs.pId
join ml.ObsHead on Obs.hdId = ObsHead.hdId
where ObsHead.name = ''SCHOOLREGDTE''
and Obs.xId = 1.e+035
and Obs.change = 2
and Obs.obsDate >= to_date('''
+ convert(varchar(30), @DateYearStart, 120)
+ ''', ''YYYY-MM-DD HH24:MI:SS'')
and Obs.obsDate < to_date('''
+ convert(varchar(30), @DateQuarterEnd, 120)
+ ''', ''YYYY-MM-DD HH24:MI:SS'')'
set @CPSQuery = 'select ic.ListName, count(distinct pp.patientprofileid) as PatCount
from PatientProfile pp
left join PatientInsurance pi on pp.PatientProfileId = pi.PatientProfileId
and pi.OrderForClaims = 1
and pi.Inactive <> 1
left join InsuranceCarriers ic on pi.InsuranceCarriersId = ic.InsuranceCarriersId
join OpenQuery(EMR_LIVE
, ''' + replace(@EMRQuery, '''', '''''') +
''' ) Students on pp.PatientId = Students.ExternalId
group by ic.ListName '
exec(@CPSQuery)
However, when I plug this in to SSRS, it doesn’t register that there are any fields available to report on. How do I convince SSRS that I do have fields to work with? Thanks.
Edit: I just declared the parameters in the query, and it recognized the field names.
declare @DateYearStart datetime
declare @DateQuarterEnd datetime
set @DateYearStart = '2011-07-01'
set @DateQuarterEnd = '2012-03-31'
Of course, that errored because I was declaring the parameters twice, once as query parameters and once within the query. But, as soon as I commented out the lines above, I lost the fields again.
Someone over at the MSDN forums suggested I select into a temp table, so I replaced the final exec statement with
and it seems to be working fine now.