0

我想带回县 ID 和县名。如何修复此查询?

DECLARE @test int = 0;


select 
CASE (@test)
    when 0 then (SELECT co.Id, co.Description
                 FROM Dictionary.Counties as co
                 INNER JOIN CountyCollaboration as cc on cc.CountyId = co.Id
                 WHERE cc.CollaborationId = (SELECT cc1.CollaborationId from CountyCollaboration as cc1
                                             WHERE cc1.CountyId = 34))
END

我得到错误only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

如果我注释掉co.Description所以我只带回来co.Id,我会得到一个不同的错误:subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >=, or when the subquery is used as as expression.

4

3 回答 3

1

您只能从 CASE 语句中返回一个表达式。尝试改用 IF/ELSE。

T-SQL 中的 CASE 语句不像许多编程语言中的 CASE/SWITCH 那样是控制流语句。

于 2013-10-30T15:54:10.940 回答
1

我建议将您的查询重组为如下所示。

select id, country 
from 
(select co.id
, co.country
, case @test  code for test goes here end caseresult
from all that stuff you have as a subquery in your question
) derivedtable
于 2013-10-30T15:58:23.907 回答
0

您的子查询返回多个字段和可能的多个记录。像这样的子查询必须总是返回一个字段和一个记录。

我猜这INNER JOIN会导致返回多条记录。如果所有记录都具有相同的值,您可以使用 aDISTINCTTOP 1

于 2013-10-30T15:58:54.737 回答