I have two tables and I want to update one row from GROUP BY data
This is my code:
2nd code:
UPDATE [TABLE].[dbo].[Movies]
SET [NumFrames] =
(SELECT COUNT(ImageURL) as "Count"
FROM [TABLE].[dbo].[Frames]
GROUP BY Movie_Id)
GO
1st code:
(SELECT COUNT(ImageURL) as "Count"
FROM [TABLE].[dbo].[Frames]
GROUP BY Movie_Id
1st code give me a row with numbers
Count
12
6
10
10
10
I want to insert it into Movies.NumFrames
2nd code give me an Error
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1
value. This is not permitted when the subquery follows =, !=, <, <=
, >, >= or when the subquery is used as an expression.
Please help
I'm sure it's simple
You need to use a WHERE-clause (GROUP BY is not necessary any more).
Otherwise your sub-query returns one row for each Movie_Id, so you have more than one row to use as source for your update.
UPDATE [TABLE].[dbo].[Movies]
SET [NumFrames] =
( SELECT COUNT(ImageURL) as "Count"
FROM [TABLE].[dbo].[Frames]
WHERE [TABLE].[dbo].[Frames].Movie_Id = [TABLE].[dbo].[Movies].Movie_Id
)
Related
select *
from employee
where controllingoffice in (
Iif(1=1, (select id from controllingoffice), (select id from controllingoffice where OwnerId=4))
)
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Note: Both subquery return more than 1 row
The reason your query fails is because your subqueries return multiple values while the iif() function is expecting scalar results for its arguments. Because iif() is a function, it evaluates to a single value and not a set of rows. You can't use it to branch as you would in a procedural language so you can't make the execution of the subqueries conditional in that sense.
The subquery below allows you to express the driving condition once in one place. That can be handy if this uses dynamic SQL. I'm presuming that your 1 = 1 is a placeholder for that input. The switching condition determines whether all rows are returned or just the ones for the single OwnerId:
select *
from employee
where controllingoffice in (
select id from controllingoffice where
<switch condition> or OwnerId = 4
);
By the way the logic X or (~X)Y reduces to X or Y
An approximation to what you intended to happen would look something like this:
select *
from employee
where
1 = 1 and controllingoffice in (
select id from controllingoffice
)
or 0 = 1 and controllingoffice in (
select id from controllingoffice where OwnerId = 4
);
I am trying to dynamically change field value using a select query based on the person's previous room number.
Below are the data sample and the query I have tried. But it is throwing an error saying that there are multiple values returned by the inner query.
,A.[Room],A.[CSR],A.[MemberShip],A.[NatCode]
,A.[MarketCode],A.[Adult],A.[Children],A.[ArrDate]
,A.[DepDate],A.[ResvStatus], CASE WHEN A.[Room]>9000 THEN (SELECT MIN(A.[Room])FROM [RESDETAILS] C WHERE C.GuestName=A.GuestName) ELSE A.[Room] END AS [Room]
FROM [ITHAAFUSHI].[dbo].[RESDETAILS] A
WHERE [GuestName]= 'Mr Jobin Joseph'
GROUP BY
A.[BusinessDate],A.[GuestName],A.[TravelAgent]
,A.[Room],A.[CSR],A.[MemberShip],A.[NatCode]
,A.[MarketCode],A.[Adult],A.[Children],A.[ArrDate]
,A.[DepDate],A.[ResvStatus]
"Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, < =,>, >= or when the subquery is used as
an expression."
Below is the expected result when Room No is >9000
Your (SELECT MIN(A.[Room])FROM [RESDETAILS] C WHERE C.GuestName=A.GuestName)
return more than 1 row so you need to use group by inside :
SELECT MIN(A.[Room])FROM [RESDETAILS] C WHERE C.GuestName=A.GuestName GROUP BY C.GuestName)
I have a table which has a lot of SSN's and that needs to be passed to a UDF that would validate it and say if its valid or not.
For example, when I execute the following query I get an error:
SELECT [dbo].[f_Is_Valid_SSN_Regex]( (select SSN from dbo.table_name))
The error that I get
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I dont think this requires a cursor (I would hate to have that), but is there a way to validate all the records on that column using this function?
Thanks,
RV
You would use a regular select for this:
SELECT SSN, [dbo].f_Is_Valid_SSN_Regex(SSN)
from dbo.table_name;
If you want to see if all of them pass, then use aggregation:
SELECT MIN(dbo.f_Is_Valid_SSN_Regex(SSN))
from dbo.table_name;
If the function return 0 for fails and 1 for passes, then this will return 1 only if all pass.
I am encountering a problem of doing calculation between table in multiple rows.
This is my code:
UPDATE StockList
SET stkQuantity = stkQuantity - (SELECT quantity FROM mCalculate)
WHERE stkID = (SELECT stkID FROM mCalculate)
If table mCalculate has only one row of data, the calculation in StockList is successfully, but if table mCalculate has multiple rows of data, I get an error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Can anyone help me solve this problem and explain to me what's the problem I am having?
Image to refer:
Window:
Database:
You have to use join instead of sub query in this case
UPDATE S
SET stkQuantity = stkQuantity - M.quantity
From stocklist s
Join mcalculate m
On s.stkid = m.stkid
Among other issues, this line here:
WHERE stkID = (SELECT stkID FROM mCalculate)
The = needs a single value, so unless your mCalculate table has only 1 row you will be getting errors.
Your error of: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. is most likely due to the WHERE clause.
Your sub-select
(SELECT quantity FROM mCalculate)
Needs a WHERE clause that ensures this select returns only one row. Ditto for this:
(SELECT stkID FROM mCalculate)
Without the SCHEMA definitions for these tables, it's hard to help you figure out exactly what that WHERE clause should be.
I am trying to update each row in a table with data from a random row from another table. Here is the SQL I am currently using:
SELECT Data, RowNumber FROM SampleData
SELECT FLOOR(ABS(CHECKSUM(NEWID())) / 2147483647.0 * 3 + 1) FROM Name
UPDATE Name SET Surname = (SELECT Data FROM SampleData WHERE RowNumber = FLOOR(ABS(CHECKSUM(NEWID())) / 2147483647.0 * 3 + 1))
And here are the results I'm getting:
Smith 1
Hunt 2
Jones 3
2
2
3
2
1
3
2
.... continues with a random number between 1 and 3 for each row in the Name table
Msg 512, Level 16, State 1, Line 9
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
So my question is why does the SELECT statement produce a single random number per row where the UPDATE sub-query seems to return multiple rows. I'm using SQL Server 2012 SP1 in case that makes a difference.
You are trying to update a field with a set of data, this leads to the described error.
Create a temporary mapping table and update name form the join between ID of name and "random" ID of SampleDate
SQLFiddle
SELECT ID,FLOOR(ABS(CHECKSUM(NEWID())) / 2147483647.0 * 3 + 1) as RN
Into #tmp
FROM Name
Update Name set SurName=Data
from #tmp
join SampleData sd on sd.RowNumber=#tmp.rn
where #tmp.ID=Name.ID
Select * from Name