2013-08-25 158 views
1

我有一個表調用的過程查找存儲的醫療程序 並有,我曾來計算手續費,所以我創造了一個動態查詢它Dyanamic SQL查詢不工作

下面

爲多個運營商表查詢

declare @TableProviderName varchar(500) 
,@SQLQuery1 nvarchar(max) 
,@MaxRecordSize Int 
,@Name varchar(250) = null  
,@code varchar(50) = null 
set @Name = 'sug' 
set @TableProviderName = 'PRD_Tata_Details' 
set @MaxRecordSize = 50 

set @SQLQuery1 = ' 
;WITH CTE_Procedure AS 
(
select top (@MaxRecordSize1) 
GPL_ID_PK as ProcedureID 
,GPL_ProcedureType as ProcedureType 
,GPL_Code as ProcedureCode 
,coalesce(Name,GPL_Name,null)as Procedurename 
,GPL_CurrencyType_FK as CurrencyType 
,ISNULL(GPL_Description,''NIL'') as ProcedureDescription 
,ISNULL(GPL_PatientInstruction,''NIL'')as PatientInstructions 
,GPL_ProcedureCategory_FK as ProcedureCategory 
,GPL_CategorySpecialization_FK as ProcedureSpecialization 
,coalesce(PatientPayable,GPL_ProcedureFee,0) as PatientPayable 
,0 as InsurancePayable 
,0 as InsuranceDiscount 
,1 as ProcedureCount 
,0 as IndBillingStatus 
,Case 
when GeneralProcedureID is not null then ''Insurance Supported'' 
else ''Insurance not Supported'' 
end as InsuranceStatus 
,ROW_NUMBER() OVER (ORDER BY GPL_Name ASC) as RowNumber 
from 
dbo.PRD_GeneralProcedure_Lookup 
left join ' 
+ @TableProviderName + 
' 
on 
GeneralProcedureID = GPL_ID_PK 
where 
GPL_ProcedureType = @ProcedureType1 
and 
(@Name1 is null or GPL_Name like %@Name1%) 
and 
(@code1 is null or GPL_Code like %@code1%) 
) 

Select 
* 
from 
CTE_Procedure 
'  

Execute sp_executesql @SQLQuery1, N'@MaxRecordSize1 int, @ProcedureType1 tinyint,@Name1 varchar(250) 
, @code varchar(50)' ,@MaxRecordSize1 = @MaxRecordSize, @ProcedureType1 = 1 , @Name1 = @Name, @code1 = @code 

但在執行錯誤發生時說 「附近有語法錯誤@名1'」

誰能幫助我與那裏的條件方面的問題

回答

2

我認爲這可能與您的like聲明以及您傳遞參數的方式有關。

看看這個問題Parameters & Like statement

@Name1 = "'%yourvalue%'" 
+1

亞,但你可以請建議中,我可以使用類似的語句 –

+1

嘗試'@名1 =「%yourvalue%'」' 當'@ Name1'解決的方法將是在正確的「like」運算符的格式。 我猜你需要爲'@ Code1'做同樣的事情 @kevinkiran – ojhawkins

+1

我發現了一個解決方案,我使用了一個表變量來存儲我從動態查詢中獲得的數據,然後使用這個表變量加入用其他表格來獲得結果。謝謝大家提供的選擇 –