【问题标题】:Dyanamic SQL Query not working动态 SQL 查询不起作用
【发布时间】:2013-08-25 10:27:10
【问题描述】:

我有一个名为procedure lookup的表,它存储医疗程序 并且有多个公司表,我必须为其计算程序费用,所以我为它创建了一个动态查询

下面是查询

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

但是当执行错误时说 "'@Name1' 附近的语法不正确"

谁能帮我解决条件方面的问题

【问题讨论】:

    标签: sql stored-procedures dynamicquery


    【解决方案1】:

    我认为这可能与您的like 语句以及您传递参数的方式有关。

    看看这个问题Parameters & Like statement

    @Name1 = "'%yourvalue%'"
    

    【讨论】:

    • 是的,但你能建议一种我可以使用 like 语句的方法
    • 尝试@Name1 = "'%yourvalue%'"@Name1 被解析时,它将采用like 运算符的正确格式。我猜你需要为@Code1@kevinkiran 做同样的事情
    • 我找到了解决方案,我使用表变量来存储从动态查询中获得的数据,然后使用此表变量与其他表连接以获得结果。感谢大家提供选择
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 2017-07-08
    • 2019-07-16
    • 2013-11-15
    • 2011-09-10
    • 2011-04-13
    相关资源
    最近更新 更多