简体   繁体   English

SQL子查询返回的值超过1

[英]SQL subquery return more that 1 value

Hi I am trying to execute a query to get row between certain number , Like i am trying to get rows between 10-20. 嗨,我正在尝试执行查询以获取特定数字之间的行,就像我正在尝试获取10-20之间的行。 so i am using subquery so that i can use row_number() function 所以我正在使用子查询,以便我可以使用row_number()函数

The query fails with the error: 查询失败,并显示以下错误:

SQL subquery return more that 1 value SQL子查询返回的值超过1

So I need to figure a way out because I need to get more that 1 resulset out of the query 所以我需要找出一个出路,因为我需要从查询中获得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

This is the full procedure and when i try to call it from the following code I get exception at _command.ExecuteReader(); 这是完整的过程,当我尝试从以下代码调用它时,在_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);
                }

The problem is this part of your sp: 问题是您的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)

Specifically, the GROUP BY dealId part. 具体来说,是GROUP BY dealId部分。 If you have multiple dealId on that table, then you are going to get multiple rows as a result. 如果该表上有多个dealId ,那么结果将是获得多行。 Obviously, you can't assign that on a scalar variable. 显然,您不能将其分配给标量变量。 Either @Count will need to be declared as a table variable (which will change the logic of the rest of your sp), or you get rid of the GROUP BY dealId , and verify that it gives you your desired results. 需要将@Count声明为表变量(这将更改sp其余部分的逻辑),或者您摆脱了GROUP BY dealId ,并验证它是否可以提供所需的结果。

I have to agree with @Lamak, and am only posting an answer so that repetition may inflict knowledge. 我必须同意@Lamak,并且仅发布答案,以便重复可能会影响知识。 If you re-write your assignment the following way, your problem will go away: 如果您通过以下方式重新编写作业,则问题将消失:

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!
;

Aside from what everyone else has stated about the subquery that populates the variable returning multiple rows, there may be a more efficient means to get at what you seek (which I'm assuming is paging). 除了关于填充返回多行变量的子查询的其他人所说的以外,可能还有一种更有效的方法来获得您想要的内容(我假设是分页的)。 This assumes that GroupId is the Primary Key of the DealData table as stated in comments: 如注释中所述,这假定GroupIdDealData表的主键:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET SQL via Petapoco &quot;子查询返回了 1 个以上的值。当子查询跟随 =、!=、&lt;、&lt;=、&gt;、&gt;= 时,这是不允许的 - .NET SQL via Petapoco "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >=" 没有子查询时,子查询返回的值超过1 - Subquery returned more than 1 value when there is no Subquery SQL:返回包含特定值的所有行不止一次 - SQL : return all rows that contain particular value more than once 启用CLR的代码+子查询返回的值超过1 - CLR enabled Code + Subquery returned more than 1 value (SQL)从2个以上的表中选择并返回所有表,这些表具有一个公共字段,其中所有表中都存在特定值 - (SQL) Select and return all from more than 2 tables that have a common field where a specific value is present in all tables tSQL错误“子查询返回了多个值...”在SqlClient中未引发异常 - tSQL error “Subquery returned more than 1 value…” does not throw an exception in SqlClient 子查询返回了 1 个以上的值。如何处理并获取多个 id - Subquery returned more than 1 value.how to handle this and get multiple ids 关联子查询SQL与LINQ - Correlated SubQuery SQL to LINQ 使用T-SQL返回多个结果集 - Return more than one result set with T-SQL 如何在子查询中基于子查询插入值 - how to insert value based on a subquery within a subquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM