Issue
I have the following stored proc. The issue I'm having is with the "conditions" parameter. Basically each condition is its own column so it it passed in like this-
@Conditions = ' AND hcc_108 = 1 AND ...' etc.
I'm trying to do something like this-
ALTER PROC [dbo].[GetPatientPanelList]
(
@CareProviderId int=null,
@Patient nvarchar(60)=null,
@Conditions varchar=null,
@LocationId int=null
)
AS
if @Conditions is null
SELECT *
FROM vw_patient_attributes t1
INNER JOIN STG_OSHODS_DW.osh_rpt.dim_member_care_measures t2
ON t1.PatientID = t2.emr_id
WHERE
(t1.PreferredServiceLocationID = @LocationId OR @LocationId IS NULL)
AND (t1.CareProviderID = @CareProviderId OR @CareProviderId IS NULL)
AND (t1.FullName like '%' + @Patient + '%' OR @Patient IS NULL)
else
SELECT *
FROM vw_patient_attributes t1
INNER JOIN STG_OSHODS_DW.osh_rpt.dim_member_care_measures t2
ON t1.PatientID = t2.emr_id
WHERE
(t1.PreferredServiceLocationID = @LocationId OR @LocationId IS NULL)
AND (t1.CareProviderID = @CareProviderId OR @CareProviderId IS NULL)
AND (t1.FullName like '%' + @Patient + '%' OR @Patient IS NULL)
+ @Conditions
I just need the last AND condition to populate based off the parameter. I understand that the " + " is the syntax error but I can't seem to figure out a way on how to implement this.
Thanks!
Update
I have tried dynamic sql but it keeps saying "Command(s) completed successfully)"
Here is my current code. I wrote this in a separate window to first get the query to work.
DECLARE @where nvarchar(50) = ' and hcc_18 = 1'
,@sql nvarchar(MAX) ,
@CareProviderId int=null,
@Patient nvarchar(60)=null,
@LocationId int=null
set @sql = 'select *
FROM vw_patient_attributes t1
INNER JOIN STG_OSHODS_DW.osh_rpt.dim_member_care_measures t2
ON t1.PatientID = t2.emr_id
WHERE t1.PreferredServiceLocationID = IsNull('+ convert(varchar,@LocationId) +',t1.PreferredServiceLocationID)
AND (t1.CareProviderID = isnull(' + convert(varchar,@CareProviderId)+ ', t1.CareProviderID)
AND (t1.FullName like %' + @Patient + '% OR ' + @Patient + ' IS NULL)' + @where
exec(@sql)
Solution
I fixed the syntax issues in your last updated. This should work...
DECLARE @where nvarchar(50) = ' and hcc_18 = 1'
,@sql nvarchar(MAX) ,
@CareProviderId int=null,
@Patient nvarchar(60)=null,
@LocationId int=null
set @sql = 'select *
FROM vw_patient_attributes t1
INNER JOIN STG_OSHODS_DW.osh_rpt.dim_member_care_measures t2
ON t1.PatientID = t2.emr_id
WHERE t1.PreferredServiceLocationID = case when '+ convert(varchar(8),isnull(@LocationId,0)) +' = 0 then t1.PreferredServiceLocationID else ' + convert(varchar,isnull(@LocationId,0)) + ' end
AND (t1.CareProviderID = case when ' + convert(varchar,isnull(@CareProviderId,0)) + ' = 0 then t1.CareProviderID else ' + convert(varchar,isnull(@CareProviderId,0)) + ' end
AND (t1.FullName like ''%' + isnull(@Patient,'') + '%'' OR ' + isnull(@Patient,0) + '=0)' + @where
print(@sql)
--exec(@sql)
Answered By - S3S Answer Checked By - Terry (sql.easybugfix Volunteer)
Post a Comment
Post a Comment