我已经查过了,但没有找到任何东西来帮助解决我的问题。我不知道如何解释与我的查询有关的错误消息。
我的问题是:
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) + ')'错误消息如下:
在期望条件的上下文中指定的非布尔类型的表达式,接近“union”。在预期条件的上下文中指定的非布尔型表达式,靠近“+ @WhereClause +”。
如何解决错误?
发布于 2014-09-23 20:03:52
你不能这样做。您将普通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))发布于 2014-09-24 00:17:15
根据其他响应,您应该执行一个Print @WhereClause并查看输出是如何格式化的,但这肯定是导致问题的@whereclause
https://stackoverflow.com/questions/26003491
复制相似问题