我可以将字符串与子查询连接起来吗?

时间:2016-06-22 18:18:51

标签: sql-server-2016

我甚至不知道如何正确地说出这个问题。我正在尝试编写一个查看案例(状态:已关闭)的查询,检查特定用户是否输入了注释,如果是,请检查是否在该注释后添加了注释(该注释将说明是否案件重新开放或不重新开放)。这就是我所拥有的:

SELECT DISTINCT scr.CaseId, scr.AssignedSRIdent, s.RepFullName FROM dbo.SupportCaseRoot scr
INNER JOIN #comparison1 c ON c.CaseId = scr.CaseId
INNER JOIN #comparison2 c2 ON c2.CaseId = scr.CaseId
INNER JOIN dbo.servicereproot s ON s.SRIdent = scr.SRIdent
INNER JOIN dbo.SupportCaseNote scn ON scn.CaseId = scr.CaseId
WHERE scr.StatusId IN (4, 36, 37, 39, 28)
AND c.DateCreated > c2.DateCreated
AND scn.Description LIKE '%status%of%' + ' ' + (SELECT scs.Description FROM dbo.SupportCaseStatus scs WHERE scs.StatusId NOT IN (4, 36, 37, 39, 28))
ORDER BY scr.CaseId

当我运行时,我收到此错误:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

我假设是因为最后一个AND语句和我在连接时的微弱尝试。我希望它能找到[找到所有描述]&#34;的状态。有人有什么建议吗?

3 个答案:

答案 0 :(得分:0)

您可以使用一些解决方案:

  • 使用'top 1'或'row offset'返回子查询中的单个值或
  • 使用聚合函数(例如ex.max)在子查询或
  • 中返回单个值
  • 使用返回单值
  • 的函数替换此子查询

答案 1 :(得分:0)

此处的错误是返回多个值的子表。

隔离子查询并查看您获得的结果。子查询最多需要返回一个结果才能使其“可连接”。

答案 2 :(得分:0)

尝试在子查询中匹配,如下所示:

SELECT DISTINCT scr.CaseId, scr.AssignedSRIdent, s.RepFullName 
FROM dbo.SupportCaseRoot scr
INNER JOIN #comparison1 c ON c.CaseId = scr.CaseId
INNER JOIN #comparison2 c2 ON c2.CaseId = scr.CaseId
INNER JOIN dbo.servicereproot s ON s.SRIdent = scr.SRIdent
INNER JOIN dbo.SupportCaseNote scn ON scn.CaseId = scr.CaseId
WHERE scr.StatusId IN (4, 36, 37, 39, 28)
AND c.DateCreated > c2.DateCreated
AND EXISTS(
  SELECT 1 FROM dbo.SupportCaseStatus scs WHERE scs.StatusId NOT IN (4, 36, 37, 39, 28) 
  AND scn.Description LIKE ('%status%of%' + ' ' + scs.Description)
)
ORDER BY scr.CaseId