0
SELECT [SKULL].[dbo].[reports].[idnumber], [SKULL].[dbo].[reports].[subid] 
FROM [SKULL].[dbo].[reports] 
WHERE reportColumn =  (SELECT DISTINCT(reportColumn)
                       FROM [SKULL].[dbo].[reports] 
                       WHERE [SKULL].[dbo].reports.reportColumn 
                       LIKE '%someword%')

我收到一条错误消息说

Msg 512, Level 16, State 1, Line 2
Subquery returned more than 1 value. 
This is not permitted when the subquery follows =, !=, <, <= , >, >= or 
when the subquery is used as an expression.

谢谢。

4

5 回答 5

2

我不太确定你想在这里做什么。如果我们假设您要返回报告列与包含相同单词的不同报告列值列表匹配的所有报告记录,那么您做的工作太多。你可以这样写:

SELECT [SKULL].[dbo].[reports].[idnumber], [SKULL].[dbo].[reports].[subid] 
FROM [SKULL].[dbo].[reports] 
WHERE reportColumn LIKE '%someword%';

如果这不是您想要的,请查看您的查询,尤其是列名和表名。如果在这种情况下您仍然无法弄清楚如何编写查询,请发布一些示例值和预期结果。

于 2012-12-22T22:46:30.857 回答
1

如果子查询返回多于一行,则使用IN而不是=as

SELECT [SKULL].[dbo].[reports].[idnumber], [SKULL].[dbo].[reports].[subid] 
FROM [SKULL].[dbo].[reports] 
WHERE reportColumn IN  (SELECT DISTINCT(reportColumn)
                       FROM [SKULL].[dbo].[reports] 
                       WHERE [SKULL].[dbo].reports.reportColumn 
                       LIKE '%someword%')

或者

如果您确定子查询返回单行,则用作

SELECT [SKULL].[dbo].[reports].[idnumber], [SKULL].[dbo].[reports].[subid] 
FROM [SKULL].[dbo].[reports] 
WHERE reportColumn =  (SELECT top(1) DISTINCT(reportColumn)
                       FROM [SKULL].[dbo].[reports] 
                       WHERE [SKULL].[dbo].reports.reportColumn 
                       LIKE '%someword%' order by idnumber)
于 2012-12-22T19:57:59.317 回答
0

错误原因:尝试将单个值与多个值进行比较。

解决方案:限制内部查询返回 1 个值或更改 where 条件以与多个返回集进行比较。

与单个值比较:SELECT [SKULL].[dbo].[reports].[idnumber], [SKULL].[dbo].[reports].[subid] FROM [SKULL].[dbo].[reports] WHERE reportColumn = (SELECT TOP1 reportColumn FROM [SKULL].[dbo].[reports] WHERE [SKULL].[dbo].reports.reportColumn LIKE '%someword%')

或者

与多个返回集比较

SELECT [SKULL].[dbo].[reports].[idnumber], [SKULL].[dbo].[reports].[subid] FROM [SKULL].[dbo].[reports] WHERE reportColumn in (SELECT DISTINCT (reportColumn) FROM [SKULL].[dbo].[reports] WHERE [SKULL].[dbo].reports.reportColumn LIKE '%someword%')

于 2012-12-22T19:30:07.633 回答
0

你正在做where field=subquery的,这是一个相等性测试——子查询必须返回一个SINGLE值,例如 1 行有 1 个值。现在是 2 行,而不是 2 个字段。如果您要返回多行,它必须是where field IN (subquery).

于 2012-12-22T19:22:49.847 回答
0

如错误所述,您的子查询只能返回一条记录。尝试使用TOP 1来做到这一点。

SELECT [SKULL].[dbo].[reports].[idnumber],  [SKULL].[dbo].[reports].[subid] FROM [SKULL].[dbo].[reports] WHERE reportColumn =  (SELECT TOP 1 reportColumn
  FROM [SKULL].[dbo].[reports] WHERE [SKULL].[dbo].reports.reportColumn LIKE '%someword%')
于 2012-12-22T19:23:44.323 回答