Hi I am trying to execute a query to get row between certain number , Like i am trying to get rows between 10-20. so i am using subquery so that i can use row_number()
function
The query fails with the error:
SQL subquery return more that 1 value
So I need to figure a way out because I need to get more that 1 resulset out of the query
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(); 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:
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. If you have multiple dealId
on that table, then you are going to get multiple rows as a result. 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.
I have to agree with @Lamak, and am only posting an answer so that repetition may inflict knowledge. 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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.