0
select  CASE when  (select distinct BR.BorrowerID from tblBorrow BR 
inner join tblWorker W on W.ContractorID=BR.BorrowerID 
inner join  tblBorrowWorker TWB on  TWB.WorkerID=W.WorkerID 
inner join tblBorrowWorkerAssign TBWA on TWB.BorrowWorkerAssignmentID=TBWA.BorrowWorkerAssignmentID where TWB.WorkerID=11276) then 'BR.BorrowerID'   else 'NotBorrowed' end as BorrowedStatus

这里实际上在'Then'条件下我想显示BorrowerID,这里的子查询部分运行良好并返回ID,但是当我添加CASE语句时,它给出一个错误“在上下文中指定的非布尔类型的表达式条件是预期的,在'then'附近。“这个查询是我的存储过程的一部分。解决方案是什么

4

2 回答 2

1

CASE 子句需要一个布尔条件。您可以使用 Exists() 检查真实情况吗?尝试将 exists() 用于内部选择语句(以 开头select distinct BR.BorrowerID from tblBorrow BR...

于 2012-05-23T06:19:29.133 回答
1

如果您的意思是显示BR.BorrowerID来自子查询而不是'BR.BorrowerID'字符串常量的值,您可以尝试以下操作(假设您已确保子查询返回不超过一行):

SELECT
  ISNULL(
    (
      SELECT DISTINCT CAST(BR.BorrowerID AS varchar(15))
      FROM tblBorrow BR 
      INNER JOIN tblWorker W ON W.ContractorID = BR.BorrowerID 
      INNER JOIN tblBorrowWorker TWB ON TWB.WorkerID = W.WorkerID 
      INNER JOIN tblBorrowWorkerAssign TBWA ON TWB.BorrowWorkerAssignmentID = TBWA.BorrowWorkerAssignmentID
      WHERE TWB.WorkerID=11276
    ),
    'NotBorrowed'
  )

另一方面,如果这是您帖子中的完整查询(不是更大查询的一部分),则以下选项也可能是一种选择:

SELECT DISTINCT ISNULL(CAST(BR.BorrowerID AS varchar(15)), 'NotBorrowed')
FROM
    (SELECT 1) x (x)
    LEFT JOIN
        tblBorrow BR 
        INNER JOIN tblWorker W ON W.ContractorID = BR.BorrowerID 
        INNER JOIN tblBorrowWorker TWB ON TWB.WorkerID = W.WorkerID 
        INNER JOIN tblBorrowWorkerAssign TBWA ON TWB.BorrowWorkerAssignmentID = TBWA.BorrowWorkerAssignmentID
    ON TWB.WorkerID = 11276

(SELECT 1) x (x)表”只是为了确保结果集不为空,以防左连接的右侧不返回任何行。

于 2012-05-23T08:55:18.940 回答