2017-08-29 1 views
1

Ich habe folgende gespeicherte Prozedur. Das Problem, das ich habe, ist mit dem Parameter "Bedingungen". Im Grunde genommen jede Bedingung ist eine eigene Spalte, so es sie übergeben wie this-SQL - Dynamische Bedingung in Where-Klausel

@Conditions = ‚UND hcc_108 = 1 UND ...‘ usw.

Ich versuche, so etwas wie this-

zu tun
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 

Ich brauche nur die letzte UND-Bedingung basierend auf dem Parameter aufzufüllen. Ich verstehe, dass das "+" der Syntaxfehler ist, aber ich kann nicht scheinen, einen Weg zu finden, wie man das umsetzt.

Danke!

aktualisiert

ich dynamische SQL habe versucht, aber es hält "Befehl (e) erfolgreich beendet)"

Hier ist mein aktueller Code zu sagen. Ich habe dies in einem separaten Fenster geschrieben, damit die Abfrage funktioniert.

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) 
+6

[Schlechte Gewohnheiten zu treten: erklärt 'varchar' ohne (Länge) - Aaron Bertrand] (http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09 /bad-habits-to-kick-declaring-varchar-without-length.aspx) - Sie sollten immer eine Länge für alle 'varchar'- oder' nvarchar'-Variablen/-Parameter angeben. - Ihre '@Conditions varchar' hat standardmäßig eine Länge von' 1'. http://rextester.com/SDHW82771 – SqlZim

+2

Struktur, die einfacher zu lesen und sollte eine bessere Leistung: 't1.PreferredServiceLocationID = isnull (@LocationId, t1.PreferredServiceLocationID)' –

+0

Gonna brauchen dynamische SQL- – scsimon

Antwort

1

Ich habe die Syntaxprobleme in Ihrem letzten aktualisiert. Dies sollte funktionieren ...

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) 
+0

Works !!! Ich danke dir sehr!! –

+0

Kein Problem @KrazyDev – scsimon