简体   繁体   中英

How to fix error: “Subquery returned more than 1 value”. Not permitted when subquery follows =, !=, <, <= , >, >= or used as an expression

The full error is:

Subquery returned more than 1 value. Not permitted when subquery follows =, !=, <, <= , >, >= or used as an expression

The query:

SqlConnection connect = Helper.GetCon;
string query = "select sum(amardate) as[All] ,"+
               "(select amardate from Amar where insertdate='" + 
                    DateTime.UtcNow.ToString("s") + "')as[Now]," +
               "(select amardate from Amar where insertdate='" +
                    DateTime.UtcNow.AddDays(-1).ToString("s") + "')as[Last] From Amar";

SqlDataAdapter da = new SqlDataAdapter(query, connect);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;

One or more of your subqueries returned multiple rows of data, in a context where it can only ever return one. eg you've got something like this happening:

                  1234          1121
sum(amardate)  +  5678 AS now + 3141 AS last
                  9101          5161

where the 1234 , 1121 , etc... are the extra rows returned by the subquery. Now what does the DB do? It has no idea WHICH of those multiple values you want to add together. It also can't just magically split this one row result into three new rows, because it has no idea HOW the split should occur.

You need to make sure the two subqueries can return only ONE result row, consisting of ONE single value.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM