2013-08-25 160 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

我发现了一个解决方案,我使用了一个表变量来存储我从动态查询中获得的数据,然后使用这个表变量加入用其他表格来获得结果。谢谢大家提供的选择 –