【问题标题】:TSQL error: An expression of non-boolean type specified in a context where a condition is expectedTSQL 错误:在预期条件的上下文中指定的非布尔类型的表达式
【发布时间】:2014-09-23 19:46:17
【问题描述】:

我已经通过 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 + '。

我该如何解决这个错误?

【问题讨论】:

  • ` where ' + @WhereClause + ' ` 不是有效的 where 子句..?
  • @whereClause 在存储过程中单独定义。
  • @where 的值是多少?只是一个例子,所以我们可以理解它的样子。但是是的 thebjorn 是正确的,您的 where 子句无效
  • set 需要在 select 之上
  • where 是否一直在变化,还是只有用户 ID 在变化?您正在执行动态 sql 但不正确。您可以将整个查询更改为一个字符串以连接@whereclause,然后执行整个字符串。但也许动态 sql 并不是你真正想要完成的。

标签: sql tsql sql-server-2012


【解决方案1】:

你不能这样做。您将普通 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))

【讨论】:

  • 查询是动态sql。我将其转换为常规 sql 以在 SO 上发布,只是为了更容易理解
  • @user3929962 您的错误消息是指您在哪里构建 SQL,而不是在哪里执行它。你没有正确转换它。
【解决方案2】:

根据其他回复,您应该执行Print @WhereClause 并查看输出的格式,但肯定是@whereclause 导致问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    相关资源
    最近更新 更多