我搜索了SO,但找不到任何可以帮助我解决问题的方法。我不知道如何解释与我的查询有关的错误消息。
我的查询是:
select AST_ID,
case when CRA_StatusID=1 then 'Wait on Info'
when CRA_StatusID=2 then 'Wait PrePay'
when CRA_StatusID=3 then 'Acquire Chart'
when CRA_StatusID=4 then 'Copy Chart'
when CRA_StatusID=5 then 'Need Invoice'
when CRA_StatusID=6 then 'Wait Payment'
when CRA_StatusID=7 then 'Ready for Delv' else 'Complete' end as AST_Status,
case when AST_WOPrinted is null then '' else 'Y' end as AST_WOPrinted,
case when AST_DeliveryDate is null then '' else 'Y' end as AST_Delivered,
AST_PatientLName+'', ''+AST_PatientFName+' '+AST_PatientMName as PatientName,
case when len(AST_RequestorName) > 0 then AST_RequestorName else AST_RequestorContact end as AST_RequestorName,
AST_Created,AST_ProviderName
from dbo.AST
inner join dbo.fnASTCurrentStatus() on AST_ID=CRA_ASTID
where ' + @WhereClause + '
union all
select AST_ID,
case
when CRA_StatusID=1 then 'Wait on Info'
when CRA_StatusID=2 then 'Wait PrePmt'
when CRA_StatusID=3 then 'Aquire Chart'
when CRA_StatusID=4 then 'Copy Chart'
when CRA_StatusID=5 then 'Need Invc'
when CRA_StatusID=6 then 'Wait Pmt'
when CRA_StatusID=7 then 'Ready for Delv' else 'Complete'
end as AST_Status,
case when AST_WOPrinted is null then '' else 'Y' end as AST_WOPrinted,
case when AST_DeliveryDate is null then '' else 'Y' end as AST_Delivered,
AST_PatientLName+'', ''+AST_PatientFName+' '+AST_PatientMName as PatientName,
case when len(AST_RequestorName) > 0 then AST_RequestorName else AST_RequestorContact end as AST_RequestorName,
AST_Created,AST_ProviderName
from dbo.Archive_AST
inner join dbo.fnArchiveASTCurrentStatus() on AST_ID=CRA_ASTID
where ' + @WhereClause + '
set @WhereClause=' AST_ProviderID in (select ProviderID from dbo.UserProvider where CSA_UserID = ' + convert(varchar,55) + ')'
错误消息显示:
在上下文中指定的非布尔类型的表达式 条件是预期的,靠近'工会'。
在上下文中指定的非布尔类型的表达式 条件是预期的,靠近'+ @WhereClause +'。
如何解决错误?
答案 0 :(得分:3)
你不能这样做。您正在将纯SQL查询与动态SQL混合使用。您必须选择是否使用动态SQL。如果您这样做,请检查EXECUTE
关键字或sp_executesql
系统存储过程。
如果是动态SQL,那么where子句变量应该在 构建查询之前发生。
您的查询可以很容易地写成:
将整个查询保持到WHERE子句和
...
WHERE AST_ProviderID in (select ProviderID from dbo.UserProvider where CSA_UserID = convert(varchar,55))
如果需要,您还可以使用变量或参数化查询:
DECLARE @myvar INT = 55;
...
WHERE AST_ProviderID in (select ProviderID from dbo.UserProvider where CSA_UserID = convert(varchar, @myvar))
答案 1 :(得分:1)
根据其他回复,您应该执行Print @WhereClause
并查看输出的格式,但它肯定是导致问题的@whereclause