您好我正在尝试执行查询以获取某个数字之间的行,就像我想要获得10-20之间的行。所以我使用子查询,以便我可以使用row_number()
函数
查询失败并显示错误:
SQL子查询返回多1个值
所以我需要想出办法,因为我需要从查询中获得更多的1结果
PROCEDURE dbo.Search
(
@search_text varchar(max),
@search_category varchar(max),
@page int,
@COUNT INT OUTPUT
)
AS
SET NOCOUNT ON
DECLARE @Lower_limit int = (@page-1)*10;
DECLARE @Upper_limit int = (@page * 10) + 1;
-- SET @COUNT =0
IF @search_category='deal'
BEGIN
SET @COUNT = (SELECT COUNT(*) FROM dealData WHERE dealInfo LIKE '%' + @search_text + '%' OR dealName LIKE '%' + @search_text + '%' OR dealDescription LIKE '%' + @search_text + '%' GROUP BY dealId);
SELECT x.dealId , x.ROW
FROM
( SELECT dealId,ROW_NUMBER() OVER(ORDER BY dealId) as ROW from dealData WHERE dealInfo LIKE '%' + @search_text + '%' OR dealName LIKE '%' + @search_text + '%' OR dealDescription LIKE '%' + @search_text + '%' GROUP BY dealId)x
WHERE x.ROW < @Upper_limit AND x.ROW > @Lower_limit
END
这是完整的过程,当我尝试从以下代码调用它时,我在_command.ExecuteReader(); Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
_query = "Search";
_command = new SqlCommand(_query, _connection);
_command.CommandType = CommandType.StoredProcedure;
_command.Parameters.AddWithValue("@search_text", search_text);
_command.Parameters.AddWithValue("@search_category", search_category);
_command.Parameters.AddWithValue("@page", page);
var returnParameter = _command.Parameters.Add("@COUNT", SqlDbType.Int);
returnParameter.Direction = ParameterDirection.Output;
_reader = _command.ExecuteReader();
while (_reader.Read())
{
search_result index = new search_result();
index.category_id = this._categoryIdFromName(search_category);
index.post_id = _reader.GetValue(0).ToString();
_searchList.Add(index);
}
答案 0 :(得分:4)
问题在于你的sp:
SET @COUNT = ( SELECT COUNT(*) FROM dealData
WHERE dealInfo LIKE '%' + @search_text + '%'
OR dealName LIKE '%' + @search_text + '%'
OR dealDescription LIKE '%' + @search_text + '%'
GROUP BY dealId)
具体来说,GROUP BY dealId
部分。如果该表上有多个dealId
,那么您将获得多行。显然,你不能在标量变量上赋值。要么@Count
需要声明为表变量(这将改变你的sp的其余部分的逻辑),或者你摆脱了GROUP BY dealId
,并验证它能为你提供所需的结果
答案 1 :(得分:2)
我必须同意@Lamak,并且只发布一个答案,以便重复可能会造成知识。如果您按照以下方式重新编写作业,您的问题就会消失:
SELECT @COUNT = COUNT(*)
FROM dbo.dealData
WHERE dealInfo LIKE '%' + @search_text + '%'
OR dealName LIKE '%' + @search_text + '%'
OR dealDescription LIKE '%' + @search_text + '%'
/* GROUP BY dealId */ -- there is NO REASON for this grouping if you want total count!
;
答案 2 :(得分:0)
除了其他所有关于填充返回多行的变量的子查询的说明之外,可能有更有效的方法来获取你所寻求的东西(我假设是分页)。这假定GroupId
是DealData
表的主键,如注释中所述:
Declare @Results Table
(
DealId ... not null Primary Key
, RowNumAsc int not null
, TotalCount int not null
);
With NumberedData As
(
Select DealId
, Row_Number() Over ( Order By GroupId ) As RowNumAsc
, Row_Number() Over ( Order By GroupId Desc ) As RowNumDesc
From DealData
Where dealInfo LIKE '%' + @search_text + '%'
Or dealName LIKE '%' + @search_text + '%'
Or dealDescription LIKE '%' + @search_text + '%'
)
Insert @Results( DealId, RowNumAsc, TotalCount )
Select DealId, RowNumAsc
, Min( RowNumAsc + RowNumDesc - 1 ) As Count
From NumberedData
Where RowNumAsc < @UpperLimit
And RowNumAsc >= @LowerLimit
Group By DealId
Set @Count = ( Select TOP 1 TotalCount From @Results );
Select DealId, RowNumAsc
From @Results