Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to table one is called category and have 2 columns type and Numberofmovies and another table called Movtype have the type and the movieID and i want update the Numberofmovies column the count number of those movies and this is my code but it gives me that error :
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


What I have tried:

update Category
set MovieNumbers = (select count(MT.MovieID)
from MovType MT inner join Category C
on  MT.[type] = C.[Type]
group by C.[Type])
Posted
Updated 19-Jan-17 13:16pm

1 solution

Try running the
select count(MT.MovieID)
from MovType MT inner join Category C
on  MT.[type] = C.[Type]
group by C.[Type])
part of your statement sparately. You will probably find it is returning mulltiple records and hence the set MovieNumber = statement will fail with your error as it can not update it to multiple values.

Also you will need a where clause at the end so only the appropiate record is updated.

Try
update Category 
set MovieNumbers = numb 

from 

(select count(MovieID) as Numb, Type from MovType group by Type) x
where x.type = category.type
 
Share this answer
 
v4
Comments
Hussien Ezzat 20-Jan-17 4:13am    
Yes i know that it will retrieve multiple values ,, what am asking is is there any way to update the whole Column of MovieNumbers with this multiple returned values at once ??
RossMW 22-Jan-17 14:17pm    
If you look closely at your statement you have
update Category set MovieNumbers = x

Your statement does not contain any where clause so it is trying to update every record with multiple values returned by your
select count(MT.MovieID)
from MovType MT inner join Category C
on MT.[type] = C.[Type]
group by C.[Type]
statement and hence the error

Try
update Category
set MovieNumbers = numb

from

(select count(MovieID) as Numb, Type from MovType group by Type) x
where x.type = category.type


I will update the solution...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900