-1

嗨,我正在尝试执行查询以获取特定数字之间的行,就像我试图获取 10-20 之间的行一样。所以我正在使用子查询,以便我可以使用row_number()函数

查询失败并出现以下错误:

SQL 子查询返回多于 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);
                }
4

3 回答 3

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,并验证它是否给你想要的结果。

于 2012-06-29T19:38:59.377 回答
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!
;
于 2012-06-29T20:41:04.410 回答
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
于 2012-06-29T21:06:05.037 回答