首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TSQL错误:在需要条件的上下文中指定的非布尔类型的表达式。

TSQL错误:在需要条件的上下文中指定的非布尔类型的表达式。
EN

Stack Overflow用户
提问于 2014-09-23 19:46:17
回答 2查看 1.6K关注 0票数 1

我已经查过了,但没有找到任何东西来帮助解决我的问题。我不知道如何解释与我的查询有关的错误消息。

我的问题是:

代码语言:javascript
复制
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 +”。

如何解决错误?

EN

回答 2

Stack Overflow用户

发布于 2014-09-23 20:03:52

你不能这样做。您将普通SQL查询与动态SQL混合在一起。如果使用或不使用动态SQL,则必须进行选择。如果这样做,请检查EXECUTE关键字或sp_executesql系统存储过程。

如果是动态SQL,那么where子句变量应该在构建查询之前发生

可以像这样轻松地编写查询:

将整个查询保持到WHERE子句和

代码语言:javascript
复制
...
WHERE AST_ProviderID in (select ProviderID from dbo.UserProvider where CSA_UserID = convert(varchar,55))

如果需要,还可以使用变量或参数化查询:

代码语言:javascript
复制
DECLARE @myvar INT = 55;
...
WHERE AST_ProviderID in (select ProviderID from dbo.UserProvider where CSA_UserID = convert(varchar, @myvar))
票数 3
EN

Stack Overflow用户

发布于 2014-09-24 00:17:15

根据其他响应,您应该执行一个Print @WhereClause并查看输出是如何格式化的,但这肯定是导致问题的@whereclause

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26003491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档