Dovberman
asked on
Subquery returned more than 1 value
I have a stack trace. How can I find out where the error occurred?
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Stack Trace:
[SqlException (0x80131904): Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.]
System.Data.SqlClient.SqlC onnection. OnError(Sq lException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1767866
System.Data.SqlClient.SqlI nternalCon nection.On Error(SqlE xception exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5352418
System.Data.SqlClient.TdsP arser.Thro wException AndWarning (TdsParser StateObjec t stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
System.Data.SqlClient.TdsP arser.TryR un(RunBeha vior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
System.Data.SqlClient.SqlD ataReader. TryHasMore Rows(Boole an& moreRows) +322
System.Data.SqlClient.SqlD ataReader. TryReadInt ernal(Bool ean setTimeout, Boolean& more) +230
System.Data.SqlClient.SqlD ataReader. Read() +34
System.Data.Common.DataAda pter.FillL oadDataRow (SchemaMap ping mapping) +64
System.Data.Common.DataAda pter.FillF romReader( DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue) +148
System.Data.Common.DataAda pter.Fill( DataSet dataSet, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords) +421
System.Data.Common.DbDataA dapter.Fil lInternal( DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +173
System.Data.Common.DbDataA dapter.Fil l(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +316
System.Data.Common.DbDataA dapter.Fil l(DataSet dataSet, String srcTable) +86
System.Web.UI.WebControls. SqlDataSou rceView.Ex ecuteSelec t(DataSour ceSelectAr guments arguments) +1481
System.Web.UI.DataSourceVi ew.Select( DataSource SelectArgu ments arguments, DataSourceViewSelectCallba ck callback) +21
System.Web.UI.WebControls. DataBoundC ontrol.Per formSelect () +138
System.Web.UI.WebControls. BaseDataBo undControl .DataBind( ) +30
System.Web.UI.WebControls. GridView.D ataBind() +4
System.Web.UI.WebControls. BaseDataBo undControl .EnsureDat aBound() +105
System.Web.UI.WebControls. CompositeD ataBoundCo ntrol.Crea teChildCon trols() +75
System.Web.UI.Control.Ensu reChildCon trols() +83
System.Web.UI.Control.PreR enderRecur siveIntern al() +42
System.Web.UI.Control.PreR enderRecur siveIntern al() +155
System.Web.UI.Control.PreR enderRecur siveIntern al() +155
System.Web.UI.Control.PreR enderRecur siveIntern al() +155
System.Web.UI.Control.PreR enderRecur siveIntern al() +155
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +974
Thanks,
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Stack Trace:
[SqlException (0x80131904): Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.]
System.Data.SqlClient.SqlC
System.Data.SqlClient.SqlI
System.Data.SqlClient.TdsP
System.Data.SqlClient.TdsP
System.Data.SqlClient.SqlD
System.Data.SqlClient.SqlD
System.Data.SqlClient.SqlD
System.Data.Common.DataAda
System.Data.Common.DataAda
System.Data.Common.DataAda
System.Data.Common.DbDataA
System.Data.Common.DbDataA
System.Data.Common.DbDataA
System.Web.UI.WebControls.
System.Web.UI.DataSourceVi
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.Ensu
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Page.Process
Thanks,
--------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I may need to rebuild the solution page by page. I was hoping that there would be a clue in the stack trace.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I found the offending stored procedure.
This is strange. The SP executes without error. This SP is the datasource for a gridview.
Here is the code:
USE [stockprosql]
GO
/****** Object: StoredProcedure [dbo].[usp_getBestPicksTop ] Script Date: 12/16/2013 14:09:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ========================== ========== =========
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: Builds the dataset for the Best Picks grid
-- ========================== ========== =========
ALTER PROCEDURE [dbo].[usp_getBestPicksTop ]
-- Add the parameters for the stored procedure here
AS
DECLARE @ToDate datetime
DECLARE @FromDate datetime
DECLARE @AsOfDate datetime
DECLARE @DaysBack int
DECLARE @MarketDays int
DECLARE @ResearchSiteURL varchar(100)
DECLARE @SiteEvalType varchar(50)
--DECLARE @UserName varchar(50)
/*
exec usp_getBestPicks
@UserID = 'BA275A68-9E77-4301-9D97-7 CC7DFECDA0 D'
@FromDate = '2009-05-01'
@ToDate = '2009-05-31',
@ResearchSiteURL = 'http://investing.money.msn.com/investments/stock-price?Symbol='
*/
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
-- Set parameters
--SET @UserID = '0A742A71-B5D5-4E00-9E5F-B D3C82B5936 A'
--SET @UserName = 'maryann'
SET @SiteEvalType = '/analyst-research/'
SET @DaysBack = 30
SET @ToDate=(SELECT MAX(QuoteDate) FROM DownLoadDates WHERE MarketID=1)
--SET @ResearchSiteURL = 'http://investing.money.msn.com/investments/stock-price?Symbol='
SET @ResearchSiteURL = 'http://www.nasdaq.com/symbol/'
SET @FromDate = (SELECT MIN(QuoteDate)
FROM
(SELECT TOP (@DaysBack) QuoteDate
FROM DownLoadDates
WHERE MarketID= 3
ORDER BY QuoteDate DESC) t);
SET @AsOfDate =(SELECT TOP 1 QuoteDate
FROM DownLoadDates
WHERE MarketID= 3
ORDER BY QuoteDate DESC)
SET ROWCOUNT 1
SELECT
w.UserName
, w.SymbolID
, @FromDate AS FromDate
, @ToDate AS ToDate
, (SELECT SymbolName FROM Symbol WHERE SymbolID = w.SymbolID) AS SymbolName
, (SELECT SUBSTRING(SecName, 1, 20) FROM Symbol WHERE SymbolID = w.SymbolID) AS SecName
, w.MarketID
, (SELECT MarketName FROM Market
WHERE MarketID = w.MarketID)
AS MarketName
, w.WatchListID
, w.PickDate
, @ResearchSiteURL AS SiteURL
, @SiteEvalType AS SiteEvalType
, (SELECT Count(*) FROM DownLoadDates
WHERE QuoteDate >=(w.PickDate +1 ) AND MarketID = 1) As DaysOnList
, (SELECT PickDatePrice FROM WatchList
WHERE PickDate=w.PickDate
AND SymbolID=w.SymbolID)
AS PickPrice
, (SELECT ClosePrice FROM StockHist
WHERE QuoteDate=@FromDate
AND SymbolID=w.SymbolID)
AS BasePrice
, (SELECT COUNT(*) FROM StockHist
WHERE ClosePrice > ClosePricePrev
AND QuoteDate BETWEEN w.PickDate AND @AsOfDate
AND SymbolID=w.SymbolID )
AS PriceUpCount
, (SELECT Top(1) ClosePrice FROM StockHist
WHERE QuoteDate=@AsOfDate
AND SymbolID=w.SymbolID)
-
(SELECT ClosePrice FROM StockHist
WHERE QuoteDate=@FromDate
AND SymbolID=w.SymbolID)
AS PriceChange
, (SELECT Top(1) ClosePrice FROM StockHist
WHERE QuoteDate=@AsOfDate
AND SymbolID=w.SymbolID)
AS CurrentPrice
, (SELECT 2* STDEV(HighPrice-LowPrice)
FROM StockHist WHERE SymbolID=w.SymbolID)
AS CI95
-- PctChange = ((CurrentPrice - BasePrice)/BasePrice) * 100
,( ( (SELECT Top(1) ClosePrice FROM StockHist
WHERE QuoteDate=@AsOfDate
AND SymbolID=w.SymbolID)
-
(SELECT ClosePrice FROM StockHist
WHERE QuoteDate=@FromDate
AND SymbolID=w.SymbolID) )
/
(SELECT ClosePrice FROM StockHist
WHERE QuoteDate=@FromDate
AND SymbolID=w.SymbolID) ) * 100
AS PctChange
FROM Watchlist w
WHERE w.UserID='0A742A71-B5D5-4E 00-9E5F-BD 3C82B5936A '
ORDER BY PctChange DESC
END
The returned more than one value error occurs when the webform containing the datasource that the gridview is based on is called.
<asp:GridView id="grdWatchList" runat="server"
Height="20px"
Width="800px" Caption="Best Picks List "
BorderColor="Black" BorderStyle="Double" BorderWidth="1px"
BackColor="Azure" AlternatingRowStyle-BackCo lor="#FFFF 66"
AlternatingRowStyle-Border Style="Sol id" Font-Names="Arial"
Font-Size="10pt" AutoGenerateColumns="False "
DataKeyNames="SymbolID" PageSize="1"
CaptionAlign="Left" ForeColor="Black" DataSourceID="dscWatchList "
AllowPaging="True"
>
There is no error if I remove "DataSourceID="dscWatchLis t"
<asp:SqlDataSource ID="dscWatchList" runat="server"
ConnectionString="<%$ ConnectionStrings:StockSel ectSQLConn ectionStri ng %>"
SelectCommand="usp_getBest PicksTop"
SelectCommandType="StoredP rocedure" >
</asp:SqlDataSource>
StoredProcedure [dbo].[usp_getBestPicksTop ] is designed to return only one row.
Problem solved!
The data is now more than 30 days old. This created an attempt to return more than one row.
Thanks,
This is strange. The SP executes without error. This SP is the datasource for a gridview.
Here is the code:
USE [stockprosql]
GO
/****** Object: StoredProcedure [dbo].[usp_getBestPicksTop
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ==========================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: Builds the dataset for the Best Picks grid
-- ==========================
ALTER PROCEDURE [dbo].[usp_getBestPicksTop
-- Add the parameters for the stored procedure here
AS
DECLARE @ToDate datetime
DECLARE @FromDate datetime
DECLARE @AsOfDate datetime
DECLARE @DaysBack int
DECLARE @MarketDays int
DECLARE @ResearchSiteURL varchar(100)
DECLARE @SiteEvalType varchar(50)
--DECLARE @UserName varchar(50)
/*
exec usp_getBestPicks
@UserID = 'BA275A68-9E77-4301-9D97-7
@FromDate = '2009-05-01'
@ToDate = '2009-05-31',
@ResearchSiteURL = 'http://investing.money.msn.com/investments/stock-price?Symbol='
*/
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
-- Set parameters
--SET @UserID = '0A742A71-B5D5-4E00-9E5F-B
--SET @UserName = 'maryann'
SET @SiteEvalType = '/analyst-research/'
SET @DaysBack = 30
SET @ToDate=(SELECT MAX(QuoteDate) FROM DownLoadDates WHERE MarketID=1)
--SET @ResearchSiteURL = 'http://investing.money.msn.com/investments/stock-price?Symbol='
SET @ResearchSiteURL = 'http://www.nasdaq.com/symbol/'
SET @FromDate = (SELECT MIN(QuoteDate)
FROM
(SELECT TOP (@DaysBack) QuoteDate
FROM DownLoadDates
WHERE MarketID= 3
ORDER BY QuoteDate DESC) t);
SET @AsOfDate =(SELECT TOP 1 QuoteDate
FROM DownLoadDates
WHERE MarketID= 3
ORDER BY QuoteDate DESC)
SET ROWCOUNT 1
SELECT
w.UserName
, w.SymbolID
, @FromDate AS FromDate
, @ToDate AS ToDate
, (SELECT SymbolName FROM Symbol WHERE SymbolID = w.SymbolID) AS SymbolName
, (SELECT SUBSTRING(SecName, 1, 20) FROM Symbol WHERE SymbolID = w.SymbolID) AS SecName
, w.MarketID
, (SELECT MarketName FROM Market
WHERE MarketID = w.MarketID)
AS MarketName
, w.WatchListID
, w.PickDate
, @ResearchSiteURL AS SiteURL
, @SiteEvalType AS SiteEvalType
, (SELECT Count(*) FROM DownLoadDates
WHERE QuoteDate >=(w.PickDate +1 ) AND MarketID = 1) As DaysOnList
, (SELECT PickDatePrice FROM WatchList
WHERE PickDate=w.PickDate
AND SymbolID=w.SymbolID)
AS PickPrice
, (SELECT ClosePrice FROM StockHist
WHERE QuoteDate=@FromDate
AND SymbolID=w.SymbolID)
AS BasePrice
, (SELECT COUNT(*) FROM StockHist
WHERE ClosePrice > ClosePricePrev
AND QuoteDate BETWEEN w.PickDate AND @AsOfDate
AND SymbolID=w.SymbolID )
AS PriceUpCount
, (SELECT Top(1) ClosePrice FROM StockHist
WHERE QuoteDate=@AsOfDate
AND SymbolID=w.SymbolID)
-
(SELECT ClosePrice FROM StockHist
WHERE QuoteDate=@FromDate
AND SymbolID=w.SymbolID)
AS PriceChange
, (SELECT Top(1) ClosePrice FROM StockHist
WHERE QuoteDate=@AsOfDate
AND SymbolID=w.SymbolID)
AS CurrentPrice
, (SELECT 2* STDEV(HighPrice-LowPrice)
FROM StockHist WHERE SymbolID=w.SymbolID)
AS CI95
-- PctChange = ((CurrentPrice - BasePrice)/BasePrice) * 100
,( ( (SELECT Top(1) ClosePrice FROM StockHist
WHERE QuoteDate=@AsOfDate
AND SymbolID=w.SymbolID)
-
(SELECT ClosePrice FROM StockHist
WHERE QuoteDate=@FromDate
AND SymbolID=w.SymbolID) )
/
(SELECT ClosePrice FROM StockHist
WHERE QuoteDate=@FromDate
AND SymbolID=w.SymbolID) ) * 100
AS PctChange
FROM Watchlist w
WHERE w.UserID='0A742A71-B5D5-4E
ORDER BY PctChange DESC
END
The returned more than one value error occurs when the webform containing the datasource that the gridview is based on is called.
<asp:GridView id="grdWatchList" runat="server"
Height="20px"
Width="800px" Caption="Best Picks List "
BorderColor="Black" BorderStyle="Double" BorderWidth="1px"
BackColor="Azure" AlternatingRowStyle-BackCo
AlternatingRowStyle-Border
Font-Size="10pt" AutoGenerateColumns="False
DataKeyNames="SymbolID" PageSize="1"
CaptionAlign="Left" ForeColor="Black" DataSourceID="dscWatchList
AllowPaging="True"
>
There is no error if I remove "DataSourceID="dscWatchLis
<asp:SqlDataSource ID="dscWatchList" runat="server"
ConnectionString="<%$ ConnectionStrings:StockSel
SelectCommand="usp_getBest
SelectCommandType="StoredP
</asp:SqlDataSource>
StoredProcedure [dbo].[usp_getBestPicksTop
Problem solved!
The data is now more than 30 days old. This created an attempt to return more than one row.
Thanks,
ASKER
Thank you.
ASKER
I modified the code but not the stored procedures.
I can look at each SELECT statement in the code.
I know about using TOP 1