![](/img/trans.png)
[英].NET SQL via Petapoco "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >="
[英]SQL subquery return more that 1 value
嗨,我正在嘗試執行查詢以獲取特定數字之間的行,就像我正在嘗試獲取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);
}
問題是您的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
,並驗證它是否可以提供所需的結果。
我必須同意@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!
;
除了關於填充返回多行變量的子查詢的其他人所說的以外,可能還有一種更有效的方法來獲得您想要的內容(我假設是分頁的)。 如注釋中所述,這假定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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.